【发布时间】: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();方法内的最后一行...... -
我认为您不需要为背景附加点击侦听器。当在其边界之外单击时,该对话框应自动关闭。