首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从AlertDialog获取结果?

如何从AlertDialog获取结果?
EN

Stack Overflow用户
提问于 2011-05-11 01:01:04
回答 5查看 41.3K关注 0票数 25

我使用AlertDialog.Builder显示一个对话框,提示用户输入密码,然后希望将该密码保存在首选项中,但是我不知道如何从警报对话框的输入法获得结果。

以下是我希望能够做的事情:

代码语言:javascript
复制
    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)方法,以某种方式设置结果并以其他方法接收结果,但我不知道如何处理最后一部分。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2011-05-11 01:31:31

代码语言:javascript
复制
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();
    }
}
票数 25
EN

Stack Overflow用户

发布于 2011-05-11 01:06:38

简化示例:

代码语言:javascript
复制
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();
}

-- EDIT --或者您可以尝试以下代码:

代码语言:javascript
复制
 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;
    }
}
票数 4
EN

Stack Overflow用户

发布于 2014-11-26 08:59:29

您可以按照developer.android.com中的Dialog教程操作。

首先创建对话框类:

Adding a list

然后将listener添加到这个类中,将结果传递给activity。

Passing Events Back to the Dialog's Host

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5953644

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档