Android的 - 自定义对话框 - 无法从获得的EditText文本 [英] Android - Custom Dialog - Can't get text from EditText

查看:323
本文介绍了Android的 - 自定义对话框 - 无法从获得的EditText文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义对话框中的一个问题。结果
我的对话框包含一个TextView,EditText上和一个确定按钮的。单击确定后,它应该得到的EditText字段中的文本,并将其分配给在活动中定义的字符串变量名。结果
一切似乎都工作,没有错误等,但是,文始终是一个空字符串。结果
我读到这样的问题的一些话题,但是我真的不知道我应该做出怎样的调整在这里。结果
我很新的到Android编程,所以我会很感激,如果某人能解释这个问题给我。先谢谢了。

 最后对话的对话=新的对话框(MyActivity.this);
     dialog.setContentView(R.layout.custom_dialog);
     dialog.setTitle(标题);     最终视图布局= View.inflate(这一点,R.layout.custom_dialog,NULL);
     Button按钮=(按钮)dialog.findViewById(R.id.dialog_ok);
     button.setOnClickListener(新OnClickListener(){
         公共无效的onClick(视图v){                编辑的EditText =(EditText上)layout.findViewById(R.id.dialog_edit);
                字符串文本= edit.getText()的toString()。                名称=文本;                dialog.dismiss();
         }
     });
    dialog.show(); }


解决方案

正在膨胀在不需要它的布局。我固定的code你看我删除了你的线在那里膨胀,改变了,你试图找到的EditText视图行了。

 最后对话的对话=新的对话框(MyActivity.this);
 dialog.setContentView(R.layout.custom_dialog);
 dialog.setTitle(标题); Button按钮=(按钮)dialog.findViewById(R.id.dialog_ok);
 button.setOnClickListener(新OnClickListener(){
     公共无效的onClick(视图v){            编辑的EditText =(EditText上)dialog.findViewById(R.id.dialog_edit);
            字符串文本= edit.getText()的toString()。            dialog.dismiss();
            名称=文本;     }
 });
dialog.show();

I have a problem with a custom dialog.
My dialog consists of a TextView, EditText and a "OK" Button. After clicking OK, it should get the text from EditText field and assign it to the String variable "name" defined in the Activity.
Everything seems to work, no errors etc, however, "text" is always an empty String.
I read some topics about such problems, however I'm not really sure what adjustments should I make here.
I'm quite new to Android programming, so I'd be grateful if sb could explain the problem to me. Thanks in advance.

     final Dialog dialog = new Dialog(MyActivity.this);
     dialog.setContentView(R.layout.custom_dialog);
     dialog.setTitle("Title");

     final View layout = View.inflate(this, R.layout.custom_dialog, null);
     Button button = (Button) dialog.findViewById(R.id.dialog_ok);
     button.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {

                EditText edit=(EditText)layout.findViewById(R.id.dialog_edit);
                String text=edit.getText().toString();

                name=text;

                dialog.dismiss();
         }
     });   


    dialog.show();

 }

解决方案

You are inflating a layout where it is not needed. I fixed your code as you see I removed your line where it inflates and changed the line where you try to find the EditText view.

final Dialog dialog = new Dialog(MyActivity.this);
 dialog.setContentView(R.layout.custom_dialog);
 dialog.setTitle("Title");

 Button button = (Button) dialog.findViewById(R.id.dialog_ok);
 button.setOnClickListener(new OnClickListener() {
     public void onClick(View v) {

            EditText edit=(EditText)dialog.findViewById(R.id.dialog_edit);
            String text=edit.getText().toString();

            dialog.dismiss();
            name=text;

     }
 });   


dialog.show();

这篇关于Android的 - 自定义对话框 - 无法从获得的EditText文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆