android - 为什么我不能在对话框 View 中为按钮设置 onClickListener?

标签 android android-layout android-emulator android-widget android-manifest

我有一个自定义对话框,如下所述。

我的自定义对话框布局 (*my_dialog.xml*) 仅包含一个“关闭”按钮:

<?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>

我的对话框 View 类:

 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);
        }


    }

正如上面的代码所示,我在对话框上有一个“*dismiss_btn*”按钮,我想在按下关闭按钮时关闭对话框,但我在我的关闭按钮的 setOnClickListener 时的代码(如代码中所示)。

错误信息(eclipse 编辑器抛出的错误):

View 类型中的方法 setOnClickListener(View.OnClickListener) 不适用于参数 (new DialogInterface.OnClickListener(){})

为什么会出现错误?为什么我不能在对话框 View 中为按钮设置 onClickListener?

最佳答案

因为 eclipse 认为它是 DialogInterface onClickListener 但你需要一个 View onClickListener 所以它会是

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

关于android - 为什么我不能在对话框 View 中为按钮设置 onClickListener?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6612229/

相关文章:

java - 字符串值是如何传递给 updateText() 方法的?

android-emulator - 如何在 Android Studio 中修复 "Could not initialize DirectSoundCapture"

Android 模拟器 - 命令行构建

android - "good"怎么是安卓模拟器?

android - 如何在现有 Activity 中加载另一个 Layout xml 文件?

android - 如何在Android中使用Google Places Api获取类别?

android - Kotlin 中 OnclickListener 方法的区别

android - 为什么线性布局不可能有样式属性

android - LinearLayout 中的 Fragment 改变了它的高度

android - 如何使用 viewpager 从选项卡布局中的 recyclerview 重新加载 fragment ?