0

我有一个像这样的警报对话框:

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            context);
    EditText input = new EditText(this);
    input.setText(sp.getString("NAME_0",""),TextView.BufferType.EDITABLE);
    alertDialogBuilder.setView(input);
    String name = input.getText().toString();
    alertDialogBuilder.setCancelable(false);
    alertDialogBuilder.setTitle("Enter your name"); //Set the title of the box
    //alertDialogBuilder.setMessage(""); //Set the message for the box
    alertDialogBuilder.setPositiveButton("Start", new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int id){
            dialog.cancel(); //when they click dismiss we will dismiss the box
        }
    });
    AlertDialog alertDialog =alertDialogBuilder.create(); //create the box
    alertDialog.show(); //actually show the box

问题是“名称”似乎总是空的。是否有其他方法可以从警报对话框内的编辑文本中获取文本?

4

2 回答 2

2

该方法工作正常,您只是过早地分配变量,例如当用户单击 onClick() 内的按钮时,您需要这样做

于 2013-07-01T19:46:02.083 回答
1

尝试以下操作:

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
        context);
EditText input = new EditText(this);
input.setText(sp.getString("NAME_0",""),TextView.BufferType.EDITABLE);
alertDialogBuilder.setView(input);
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setTitle("Enter your name"); //Set the title of the box
//alertDialogBuilder.setMessage(""); //Set the message for the box
alertDialogBuilder.setPositiveButton("Start", new DialogInterface.OnClickListener(){
    public void onClick(DialogInterface dialog, int id){
        String name = input.getText().toString();
        dialog.cancel(); //when they click dismiss we will dismiss the box
    }
});
AlertDialog alertDialog =alertDialogBuilder.create(); //create the box
alertDialog.show(); //actually show the box

您在创建对话框时尝试了 EdtiText 的值,该对话框为空。

于 2013-07-01T19:47:12.353 回答