如何从AlertDialog获取结果?

时间:2011-05-10 17:01:04

标签: android android-edittext alertdialog

我正在使用AlertDialog.Builder来显示一个提示用户输入密码的对话框,然后我想在偏好中保存该密码,但我无法弄清楚如何从警报对话框中获取结果输入法。

这基本上是我希望能够做到的:

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

但是,我愿意做showDialog(int);这样的事情然后使用onCreateDialog(int)方法并以某种方式设置结果并以其他方法接收它,但我不知道如何去做最后一部分。

6 个答案:

答案 0 :(得分:24)

public class MyActivity extends Activity {
    private String result;

    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) {
                result = input.getText().toString();
            }
        });
        b.setNegativeButton("CANCEL", null);
        b.show();
    }
}

答案 1 :(得分:3)

简化示例:

public interface TextListener {
    void onPositiveResult(CharSequence text);
}

public static AlertDialog getTextDialog(Context ctx,
        final TextListener listener) {
    View view = LayoutInflater.from(ctx).inflate(R.layout.dialog, null);
    final TextView tv = (TextView) view.findViewById(R.id.tv);
    AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
    builder.setView(view);
    //
    builder.setPositiveButton(android.R.string.ok, new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            listener.onPositiveResult(tv.getText());
        }
    });
    builder.setNegativeButton(android.R.string.cancel, null);
    return builder.create();
}

- 编辑 - 或者你试试这段代码:

 public class Main extends Activity {
    private Button btn;
    private String result;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                showDialog(0);
            }
        });
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case 0:
            final EditText input = new EditText(this);

            return new AlertDialog.Builder(this)
            .setIcon(R.drawable.icon)
            .setTitle("Please enter a password")
            .setView(input)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int whichButton) {
                    Toast.makeText(getBaseContext(), input.getText().toString(), Toast.LENGTH_SHORT).show();
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int whichButton) {
                    Toast.makeText(getBaseContext(), "Cancel clicked!", Toast.LENGTH_SHORT).show();
                }
            }).create();
        }

        return null;
    }
}

答案 2 :(得分:2)

您可以关注developer.android.com上的Dialog教程。

首先创建对话框类:

Adding a list

然后将侦听器添加到此类以将结果传递给活动。

Passing Events Back to the Dialog's Host

答案 3 :(得分:1)

变量结果,使其成为成员变量,而不是局部变量。通过将“result”作为成员变量,可以在整个活动中访问它(扩展活动的父类)

答案 4 :(得分:1)

弱但快速的解决方案:

声明一个活动级别方法:

TextView lblDiaDestination;

public void dialogReturn(String msg){
   lblDiaDestination.setText(msg);
}

并从正按钮点击列表器中调用它:

alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        dialogReturn(   input.getText().toString().trim()   );
    });

您可以在alert.show()之前更改lblDiaDestination值:

lblDiaDestination = myTextView;
alert.show():

答案 5 :(得分:0)

这很长。我希望我的贡献对新来者有用。 我认为回答这个问题的关键是将变量结果声明为活动的成员(让我们称之为 YourActivity )。然后从“onClick”方法内部使用 YourActivity.this.result 来访问此变量。

我编辑@finiteloop代码如下:

public class YourActivity 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) {        
               YourActivity.this.result = input.getText().toString();
            }
        });
        b.setNegativeButton("CANCEL", null);
        b.create().show();
    }
}