【问题标题】:Save value from dialogue box using AlertDialog.Builder使用 AlertDialog.Builder 保存对话框中的值
【发布时间】:2013-08-01 13:37:21
【问题描述】:

我是一名新的 Android 开发人员,有一个可能很愚蠢的问题。
在我的活动创建中,我想制作一个弹出对话框来提示用户输入。然后我想获取该输入并将其保存为全局变量以供以后使用。目前我有

 public class MgenActivity extends Activity {

// Instance Variables
String ip = "";

/**
 * On Create
 */
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    String box = dialogBoxStart();
    this.ip = box;
    //more code 
    }

public String dialogBoxStart() 
{
    String returned = "";
    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("MGEN Setup");
    alert.setMessage("Please enter the MGEN IP address such as \n 9.42.68.69");

    // Set an EditText view to get user input
    final EditText input = new EditText(this);
    alert.setView(input);

    DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() 
    {
            public void onClick(DialogInterface dialog, int whichButton) 
            {
            //send user input to global?
            //this doesnt work: this.ip  = input.getText().toString();
            }
    };

    alert.setPositiveButton("Ok", listener);

    alert.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    // Canceled.
                }
            });
    returned =  input.getText().toString();
    alert.show();
    return returned;

}

问题是我的 EditText 'input' 没有保存用户输入,所以我无法向外保存。同样,我无法从我的 onClick 方法中与我的外部变量交谈。

那么 TLDR:您如何保存来自对话框的输入?

【问题讨论】:

    标签: android input io android-alertdialog


    【解决方案1】:

    就这么简单。

    DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            ip = input.getText().toString();
        }
    };
    

    【讨论】:

      【解决方案2】:

      对话框不会阻塞代码执行或 UI 线程。您显示对话框,用户单击确定按钮,您将值设置为全局变量,但您使用它的代码已经执行。您当前的实现也是如此。 dialogBoxStart() 返回空值,因为它在用户输入任何内容之前获取输入值。

      检查这个问题:Dialogs / AlertDialogs: How to "block execution" while dialog is up (.NET-style)

      【讨论】:

        【解决方案3】:
        new AlertDialog.Builder(Main.this)
            .setTitle("Update Status")
            .setMessage(message)
            .setView(input)
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    Editable value = input.getText(); 
                }
            }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    // Do nothing.
                }
            }).show();
        

        【讨论】:

          猜你喜欢
          • 2014-11-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多