机器人 - 获取手柄组件在自定义对话框 [英] Android - Getting the handle to components in a Custom Dialog

查看:150
本文介绍了机器人 - 获取手柄组件在自定义对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在菜单中的选项,点击创建一个自定义对话框。我去开始一个新的活动,意图指向customTextMessageDialog。

我是能够得到处理以任何使用code,如phoneNumber的=(EditText上)dialog.findViewById在我customdialog.xml文件中定义的小部件(R.id.customDialogPhoneNumber1 );

然而,我无法与该句柄做任何事情。我尝试的setText()的一个EditText手柄或那一刻,如果我设置一个onClickListener在我的XML我的应用程序崩溃定义的按钮中的一个。任何想法,为什么发生这种情况,我怎么能解决这个问题?

另外,为什么我需要之前,先在该实例上调用findViewById创建对话框的实例?

 公共类customTextMessageDialog扩展活动
{
    的EditText phoneNumber的;
    的EditText消息;
    按钮cancelButton;
    按钮sendButton;

公共无效的onCreate(包savedInstanceState)
{
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.customdialog);
    对话对话框=新的对话框(本);
    phoneNumber的=(EditText上)dialog.findViewById(R.id.customDialogPhoneNumber1);
    //phoneNumber.setTextColor(R.color.gray);
    //phoneNumber.setText("ex:4127214544);
    消息=(EditText上)dialog.findViewById(R.id.customDialogMessage1);
    cancelButton =(按钮)dialog.findViewById(R.id.customDialogCancelButton1);
    cancelButton.setOnClickListener(新OnClickListener()
    {
        @覆盖
        公共无效的onClick(视图v){
            完();

        }
    });
    sendButton =(按钮)dialog.findViewById(R.id.customDialogPhoneNumber1);
}
}
 

解决方案

在你的code您正在设置活动的内容查看到 customDialog 。您需要在对话框的布局设置为布局。阅读

做到这一点:

  dialog.setContentView(R.layout.customdialog);
 

I am trying to create a custom dialog box on the click of an option in the menu. I go on to start a new activity with the Intent pointing to customTextMessageDialog.

I am able to get the handle to any of the widgets defined in my customdialog.xml file using code like "phoneNumber = (EditText)dialog.findViewById(R.id.customDialogPhoneNumber1);"

However, I can't do anything with this handle. The moment I try to setText() for an EditText handle or if I set an onClickListener for one of the buttons defined in my xml my application crashes. Any idea as to why this is happening and how I can fix this?

Also, why do I need to create an instance of Dialog first before calling findViewById on that instance?

public class customTextMessageDialog extends Activity
{
    EditText phoneNumber;
    EditText message;
    Button cancelButton;
    Button sendButton;

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.customdialog);
    Dialog dialog = new Dialog(this);
    phoneNumber = (EditText)dialog.findViewById(R.id.customDialogPhoneNumber1);
    //phoneNumber.setTextColor(R.color.gray);
    //phoneNumber.setText("ex: 4127214544");
    message = (EditText)dialog.findViewById(R.id.customDialogMessage1);
    cancelButton = (Button)dialog.findViewById(R.id.customDialogCancelButton1);
    cancelButton.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v) {
            finish();

        }
    });
    sendButton = (Button)dialog.findViewById(R.id.customDialogPhoneNumber1);
}
}

解决方案

In your code you are setting the Activity's contentView to customDialog. You need to set the layout of your dialog to that layout. Read this.

Do this:

dialog.setContentView(R.layout.customdialog);

这篇关于机器人 - 获取手柄组件在自定义对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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