为什么我不能设置onClickListener在对话框视图按钮? [英] Why I can not set onClickListener for a button in a dialog view?

查看:143
本文介绍了为什么我不能设置onClickListener在对话框视图按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下所述的自定义对话框。

I have a custom dialog which described below.

我的自定义对话框布局( * my_dialog.xml * )只包含了解雇按钮:

My Custom Dialog layout (*my_dialog.xml*) which only contain a "dismiss" button:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"

     >

         <Button
            android:id="@+id/dismiss_btn"
            android:layout_width="100dip"
            android:layout_height="30dip"
            android:layout_centerHorizontal="true"
            android:text="Dismiss me"
            android:textSize="8dip"
            android:textColor="#ffffff"
             />
     </RelativeLayout>

我的对话视图类:

my dialog view class:

 public class MyDialog extends Dialog{

        public MyDialog(Context context){
            super(context);
        }


        @Override
        protected void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.my_dialog);

                 Button dismissMeBtn = (Button)findViewById(R.id.dismiss_btn);

                /** ERROR Message when set onClickListener below (throw by eclipse editor)**/

               //     The method setOnClickListener(View.OnClickListener) in the 
                //    type View is not applicable for the arguments (new DialogInterface.OnClickListener(){})

                dismissMeBtn.setOnClickListener(new OnClickListener() { 
                    @Override
                    public void onClick(View v) {
                         MyDialog.this.dismiss(); 
                    }
                  });

            getWindow().setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        }


    }

由于code所示上面,我有一个 * dismiss_btn * 对话框中的按钮,我想关闭该对话框时,解雇按钮是pressed,但我在code得到一个错误时的 setOnClickListener 作为解雇按钮(如code所示)。

As the code indicated above, I have a "*dismiss_btn*" button on the dialog, I would like to dismiss the dialog when the dismiss button is pressed, but I got an error in my code when setOnClickListener for the dismiss button (as indicated in the code).

错误信息(由Eclipse编辑器错误掷):

Error message (error throw by eclipse editor):

在类型视图的方法setOnClickListener(View.OnClickListener)不适用于参数(新DialogInterface.OnClickListener(){})

为什么我得到了错误?为什么我不能设置onClickListener一个按钮,在对话框的看法?

Why I got the error? Why I can not set onClickListener for a button in a dialog view?

推荐答案

由于日食认为这是DialogInterface onClickListener,但你需要查看onClickListener所以这将是

As eclipse thinks it is DialogInterface onClickListener but you need a View onClickListener so it will be

                dismissMeBtn.setOnClickListener(new View.OnClickListener() { 
                @Override
                public void onClick(View v) {
                     MyDialog.this.dismiss(); 
                }
              });

这篇关于为什么我不能设置onClickListener在对话框视图按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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