2017-05-01 103 views
-1

我遇到了顯示警告框的代碼部分,要求用戶輸入數據集的名稱。內部存儲器中將創建一個具有相同名稱的文件。警報對話框工作正常,但在SD卡中創建的文件始終爲「null.txt」。經過幾個小時的搜索之後,我嘗試了所有的解決方案,例如爲警告框創建自定義佈局,向edittext等添加TextWatcher,但沒有任何幫助。當我手動將數據集名稱分配給變量'Set'時,該文件被成功創建。因此,alertdialog存在問題。我正在使用android studio 2.3.1。請幫助。Android警報對話框Edittext值不顯示

我也在發佈代碼。

else if (map2.contains("1") || map2.contains("4")) { 
    final EditText edittext = new EditText(Scan.this); 
    AlertDialog.Builder alert1 = new AlertDialog.Builder(Scan.this); 
    alert1.setMessage("Name of the dataset"); 
    alert1.setTitle("Enter Your Title"); 
    alert1.setView(edittext); 
    alert1.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 
      String set = edittext.getText().toString(); 
      Set = set; 
     } 
    }); 
    alert1.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){ 
     public void onClick(DialogInterface dialog, int whichButton) { 
     } 
    }); 
    alert1.show(); 
} 
myFile = new File(Environment.getExternalStorageDirectory(),Set+".txt"); 
if (!myFile.exists()) { 
    try { 
     myFile.createNewFile(); 
     FileOutputStream fOut = new FileOutputStream(myFile); 
     OutputStreamWriter osw = new OutputStreamWriter(fOut); 
     osw.write(message1); 
     osw.flush(); 
     osw.close(); 
     fOut.close(); 
    } 
    catch (Exception e) { 
     e.printStackTrace(); 
    } 
    finally { 
    } 
} 

回答

0

試試這個代碼:

膨脹含Edittext您的自定義佈局,並使用它像:

LayoutInflater inflater = LayoutInflater.from(Scan.this);    
final View view = inflater.inflate(R.layout.custom_layout, null); 
AlertDialog.Builder alert = new AlertDialog.Builder(Scan.this); 

alert.setTitle("Tilte"); 
alert.setMessage("Name of the dataset"); 
alert.setView(view); 

final EditText et1 = (EditText) view.findViewById(R.id.editText1); 

alert.setPositiveButton("ok", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int whichButton) 
    { 
      String s1=et1.getText().toString(); 
      //do operations using s1... 
      myFile = new File(Environment.getExternalStorageDirectory(),s1+".txt"); 
      if (!myFile.exists()) { 
       try { 
        myFile.createNewFile(); 
        FileOutputStream fOut = new FileOutputStream(myFile); 
        OutputStreamWriter osw = new OutputStreamWriter(fOut); 
        osw.write(message1); 
        osw.flush(); 
        osw.close(); 
        fOut.close(); 
       } 
       catch (Exception e) { 
        e.printStackTrace(); 
       } 
       finally { 
       } 
     } 
}); 

alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int whichButton) { 
      dialog.cancel(); 
    } 
}); 

alert.show(); 

從@Deepti Maduskar

最後我能解決這個問題。問題在於全球可變集合的定義。我只需要聲明它,而不是將其定義爲final

+0

試過,但沒有工作。 –

+0

在你的'onClick'裏面打印一個日誌,看看你是否得到這個值... – rafsanahmad007

+0

是的值被分配,但是在警報對話框可以傳遞值之前創建文件。 –

1

您在對話框中設置標題之前創建文件。 試試這個:

else if (map2.contains("1") || map2.contains("4")) { 
    final EditText edittext = new EditText(Scan.this); 
    AlertDialog.Builder alert1 = new AlertDialog.Builder(Scan.this); 
    alert1.setMessage("Name of the dataset"); 
    alert1.setTitle("Enter Your Title"); 
    alert1.setView(edittext); 
    alert1.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 
      String set = edittext.getText().toString(); 
      createFile(set); 
     } 
    }); 
    alert1.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){ 
     public void onClick(DialogInterface dialog, int whichButton) { 
     } 
    }); 
    alert1.show(); 
} 
} 
private void createFile(String set){ 
    myFile = new 
File(Environment.getExternalStorageDirectory(),set+".txt"); 
    if (!myFile.exists()) { 
     try { 
      myFile.createNewFile(); 
      FileOutputStream fOut = new FileOutputStream(myFile); 
      OutputStreamWriter osw = new OutputStreamWriter(fOut); 
      osw.write(message1); 
      osw.flush(); 
      osw.close(); 
      fOut.close(); 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 
     finally { 
     } 
    } 

}

0

你給的權限清單:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

請檢查此創建.txt文件。

String editString = mEditText.getText().toString(); 
    filename = fileName_mEditText.getText().toString(); 

    if (filename.trim().equalsIgnoreCase("")) { 
     fileName_mEditText.setError("Enter File Name!"); 
    }else if (editString.trim().equalsIgnoreCase("")) { 
     mEditText.setError("Enter Text!"); 
    } else { 
     try { 
      FileOperations fop = new FileOperations(); 
      fop.write(filename, editString); 
      if (fop.write(filename, editString)) { 
       Toast.makeText(getApplicationContext(), filename + ".txt created", Toast.LENGTH_SHORT).show(); 
      } else { 
       Toast.makeText(getApplicationContext(), "I/O error", Toast.LENGTH_SHORT).show(); 
      } 
      } catch (Exception e) { 
        e.printStackTrace(); 
      } 
     } 
    } 
+0

當我分配相同的「設置」變量一些其他它的價值工作正常。該文件正在創建。 –