【问题标题】:how to dismiss dialog without using cancel? [duplicate]如何在不使用取消的情况下关闭对话框? [复制]
【发布时间】:2016-12-17 17:46:59
【问题描述】:

此对话框有两个选项。我想通过触摸背景来关闭对话框,但我不知道怎么做。

public void dialog(  long id) {
    final int     position=(int) id;
        final Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);      

        dialog.setContentView(R.layout.dialog_checkbox);

        // set the custom dialog components - text, image and button

        Button yes = (Button) dialog.findViewById(R.id.yes);
        Button no = (Button) dialog.findViewById(R.id.no);

        yes.setTypeface(type);
        no.setTypeface(type);

        // if button is clicked, close the custom dialog
        yes.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

            String number=contactnumber.get(position);
            String name=contactname.get(position);
                 contactnumber.remove(position);
                 contactname.remove(position);
                 adapter.notifyDataSetChanged();
                 dialog.dismiss();
                 str="del";
                 delete(name, number);

            }});

        no.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {


                edit(position);
                dialog.dismiss();

                }});
dialog.show();
DisplayMetrics metrics = getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
dialog.getWindow().setLayout((6 * width)/7, LayoutParams.WRAP_CONTENT);





    }

我用这个但是不行

LinearLayout background = (LinearLayout) findViewById(R.id.back);
background.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        SetBackground();
     // here I want to dismiss it after SetBackground() method 
      OptionDialog.dismiss();
    }
});

未定义类型 new View.OnClickListener() 的方法 SetBackground()

【问题讨论】:

  • 调用OptionDialog.dismiss();方法内的最后一行......
  • 我认为您不需要为背景附加点击侦听器。当在其边界之外单击时,该对话框应自动关闭。

标签: android eclipse dialog


【解决方案1】:

你可以使用 dialog.setCanceledOnTouchOutside(true);如果您在对话框之外触摸,它将关闭对话框。

更多信息请看这里

How to dismiss the dialog with click on outside of the dialog?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    • 1970-01-01
    • 2017-01-10
    • 2014-09-26
    相关资源
    最近更新 更多