【问题标题】:Where do I define my OnClickListener to close a custom dialog view in Android?我在哪里定义我的 OnClickListener 以关闭 Android 中的自定义对话框视图?
【发布时间】:2011-09-30 12:00:47
【问题描述】:

我已经定义了以下自定义对话框视图:

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

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

对话框使用以下布局,其中仅包含一个“关闭我”按钮:

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

我的MainActivity 显示一个按钮triggerDialogBtn,按下该按钮将显示我的对话框。我还为对话框上的按钮 dismiss_btn 定义了一个处理方法,该方法旨在在按下时关闭我的对话框。

public class MainActivity extends Activity{
    private Button triggerDialogBtn;
    private MyDialog myDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //initialize my dialog
        myDialog = new MyDialog(this);

        //the button which will trigger the dialog to pop up
        Button triggerDialogBtn = (Button)findViewById(R.id.trigger_btn);
        triggerDialogBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                myDialog.show(); //my dialog pop up
            }
        });

        // I would like to dismiss my dialog when the "Dismiss me" button on the dialog is pressed
        /**** BUT I GOT NULL POINTER EXCEPTION HERE ******/

        //This is the "Dismiss me" button defined on dialog layout
        Button dismissMeBtn = (Button)findViewById(R.id.dismiss_btn); 
        dismissMeBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                 myDialog.dismiss(); 
            }
        });
    }
}

main.xml的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <!-- The trigger button which will pop up my dialog when pressed -->
    <Button
        android:id="@+id/trigger_btn"
        android:layout_width="80dip"
        android:layout_height="20dip"
        android:text="Trigger dialog" />
</LinearLayout>

问题是,如果我在MainActivity 内定义dismiss_btn(位于我的对话框中)的点击处理程序,则在按下dismiss_btn 时我无法关闭对话框;它总是抛出NullPointerException

那么,当我按下对话框上的按钮时,我可以在哪里以及如何定义处理程序以关闭我的对话框?

更新

我还尝试将dismiss_btn 处理程序放在我的自定义对话框视图类中,如下所示:

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: 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) {
                ???.dismiss(); // What should replace "???"
            }
        });

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

我收到指示的错误,不知道用什么替换问号。

【问题讨论】:

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


    【解决方案1】:

    您的活动的内容视图(在 onCreate 开始时设置)是布局“主”,并且您试图从该活动完全访问另一个布局中的视图 (dismiss_btn)。尝试移动此代码:

      Button dismissMeBtn = (Button)findViewById(R.id.dismiss_btn); //This is the "Dismiss me" button defined on dialog layout
      dismissMeBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                 /* Changed to reflect access from listener within MyDialog */
                 MyDialog.this.dismiss(); 
            }
          });
    

    进入您的对话框 - 根据需要进行编辑。

    【讨论】:

    • @Nathan ,我做到了,但仍有错误,请查看我上面更新的帖子。
    • 正如巴拉吉所说,您可以调用 MyDialog.this.dismiss()
    • 编辑了我的答案以反映这一点。
    • 阅读本文以更好地了解正在发生的事情 :) stackoverflow.com/questions/2076037/…
    • 正如您发现的那样,onClickListener 不止一种 - 要指定 View onClickListener 的使用,请将您的代码更改为“new View.onClickListener() { ...”
    【解决方案2】:

    你可以这样使用:

    MyDialog.this.dismiss();

    【讨论】:

    • 但我仍然收到错误:错误:View 类型中的方法 setOnClickListener(View.OnClickListener) 不适用于参数(新 DialogInterface.OnClickListener(){})
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-22
    相关资源
    最近更新 更多