Android Alert对话框未显示Edittext值

时间:2017-05-01 09:34:35

标签: android android-alertdialog

我遇到了代码的一部分,其中显示了一个警告框,要求用户输入数据集的名称。将在内部存储中创建同名文件。警报对话框工作正常,但在SD卡中创建的文件始终为“null.txt”。经过几个小时的搜索,我尝试了所有解决方案,例如为警报框创建自定义布局,向TextWatcher等添加edittext,但没有任何帮助。当我手动将数据集的名称分配给变量“Set”时,文件已成功创建。因此alertdialog存在问题。我正在使用android studio 2.3.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();
            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 {
   }
}

3 个答案:

答案 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 {
       }
   }

}

答案 1 :(得分: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

答案 2 :(得分:0)

您是否在Manifest中授予了权限:

<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();
            }
       }
  }