我怎样才能得到一个AlertDialog的结果?结果、AlertDialog

2023-09-04 06:23:50 作者:霸剑横空

我使用的是AlertDialog.Builder显示一个对话框,提示用户输入密码,然后我要保存密码在preference,但我无法弄清楚如何从结果警告对话框中的输入法。

下面基本上是什么,我希望能够做到:

 字符串的结果;
    AlertDialog.Builder B =新AlertDialog.Builder(本);
    b.setTitle(请输入密码);
    最后的EditText输入=新的EditText(本);
    b.setView(输入);
    b.setPositiveButton(OK,新DialogInterface.OnClickListener()
    {
        @覆盖
        公共无效的onClick(DialogInterface对话,诠释whichButton)
        {
           //我在这里得到一个编译错误,它想结果为最终决定。
           。结果= input.getText()的toString();
        }
    });
    b.setNegativeButton(取消,NULL);
    。b.create()显示();
 

不过,我愿意做的事情,如的ShowDialog(INT); 然后用 onCreateDialog(INT)方法并以某种方式设定的结果和接收它在其他一些方法,但我不知道如何去的最后部分。

解决方案

 公共类胡说延伸活动{
    私人字符串的结果;

    //其他活动的东西

    无效的ShowDialog(){
    AlertDialog.Builder B =新AlertDialog.Builder(本);
    b.setTitle(请输入密码);
    最后的EditText输入=新的EditText(本);
    b.setView(输入);
    b.setPositiveButton(OK,新DialogInterface.OnClickListener()
    {
        @覆盖
        公共无效的onClick(DialogInterface对话,诠释whichButton)
        {
           //现在应该可以工作
           。结果= input.getText()的toString();
        }
    });
    b.setNegativeButton(取消,NULL);
    。b.create()显示();
    }
}
 
对话框AlertDialog的使用

I am using an AlertDialog.Builder to display a dialog to prompt the user to enter a password, I then want to save that password in a preference, however I can't figure out how to get the result from the alert dialog's input method.

Here is essentially what I would like to be able to do:

    String result;
    AlertDialog.Builder b = new AlertDialog.Builder(this);
    b.setTitle("Please enter a password");
    final EditText input = new EditText(this);
    b.setView(input);
    b.setPositiveButton("OK", new DialogInterface.OnClickListener()
    {
        @Override
        public void onClick(DialogInterface dialog, int whichButton)
        {
           //I get a compile error here, it wants result to be final.
           result = input.getText().toString();
        }
    });
    b.setNegativeButton("CANCEL", null);
    b.create().show();

However, I am open to doing something such as showDialog(int); then using the onCreateDialog(int) method and somehow setting the result and receiving it in some other method, but I have no idea how to go about the last part.

解决方案

public class Blah extends Activity {
    private String result;

    // other activity stuff

    void showDialog(){
    AlertDialog.Builder b = new AlertDialog.Builder(this);
    b.setTitle("Please enter a password");
    final EditText input = new EditText(this);
    b.setView(input);
    b.setPositiveButton("OK", new DialogInterface.OnClickListener()
    {
        @Override
        public void onClick(DialogInterface dialog, int whichButton)
        {
           // SHOULD NOW WORK
           result = input.getText().toString();
        }
    });
    b.setNegativeButton("CANCEL", null);
    b.create().show();
    }
}

 
精彩推荐
图片推荐