首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从customDialog获取值EditText

如何从customDialog获取值EditText
EN

Stack Overflow用户
提问于 2012-06-02 16:00:25
回答 2查看 339关注 0票数 0

我的场景是,在主屏幕上有一个按钮,当用户单击该按钮时,会出现一个对话框窗体,其中包含一个textedit和2个按钮。我的问题是,当我试图从编辑文本中获取值时,似乎什么都没有发生,并且值始终为空。

这是我的代码:我在main活动中声明对话框

代码语言:javascript
复制
private void popup() {
        AlertDialog.Builder builder;
        AlertDialog alertDialog;

        LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
        View dialog = inflater.inflate(R.layout.isbn_dialog,
                                       (ViewGroup) findViewById(R.id.layout_root));

                    // The edit text from dialog
        isbnInput = (EditText)dialog.findViewById(R.id.isbn);

        builder = new AlertDialog.Builder(this);
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(), isbnInput.getText().toString(), Toast.LENGTH_LONG);
            }
        });

        builder.setView(dialog);
        alertDialog = builder.create();
        alertDialog.setTitle("Enter ISBN Number");
        alertDialog.show();
}

那么,我如何才能从对话框中正确获取编辑文本的值呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-06-02 16:08:24

这行必须是,

代码语言:javascript
复制
 Toast.makeText(getApplicationContext(), isbnInput.getText().toString(), Toast.LENGTH_LONG).show();

你忘了点击Toast inside按钮的show() ..

试试看..

票数 1
EN

Stack Overflow用户

发布于 2012-06-02 16:09:03

我没有看到你对AlertDialog的编辑文本引用。

尝试一下,和您一样,可以肯定的是我可以正确地获取editText

代码语言:javascript
复制
    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setMessage(getResources().getString(R.string.my_email));
    final EditText input = new EditText(this);



    final LinearLayout layout = new LinearLayout(this);
    LayoutParams params = new LayoutParams(
            (int) getResources().getDimension(R.dimen.simple_alert_width_normal),
            LayoutParams.WRAP_CONTENT);
    params.setMargins(10, 0, 10, 0);
    layout.addView(input, params);

    alert.setView(layout);
    alert.setPositiveButton(R.string.OK,
            new DialogInterface.OnClickListener() {
            // DO some thing here

            });

    alert.setNegativeButton(R.string.cancel,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    // Canceled.

                }
            });

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

https://stackoverflow.com/questions/10860762

复制
相关文章

相似问题

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