如何在不使用取消的情况下关闭对话框?

时间:2016-08-11 06:23:20

标签: android eclipse dialog

此对话框有两个选项。我想通过触摸背景来解除对话,但我不知道如何。

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

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

1 个答案:

答案 0 :(得分:0)

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

有关详细信息,请查看此

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