我有一个没有按钮的AlertDialog。点击它时如何关闭对话框?
答案 0 :(得分:3)
我能想到的问题是将自己的视图设置为该对话框,然后可以将onClickListener设置为该视图,这样就可以处理click事件。 以下是我的代码:
@Override
protected Dialog onCreateDialog(int id, Bundle args) {
AlertDialog.Builder builder=new Builder(this);
builder.setTitle("title");
LayoutInflater inflater=(LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=inflater.inflate(R.layout.firstview, null);//inflate your own view
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
YourActivity.this.dismissDialog(dialog_ID);//dismiss the dialog
}
});
builder.setView(view);//set your custom view to your dialog
return builder.create();
}
答案 1 :(得分:1)
这将关闭用户在对话框外触摸的对话框,因此无需按钮:
dialog.setCanceledOnTouchOutside(true);
答案 2 :(得分:0)
你必须为它创建自定义对话框。看下面的链接,它会帮助你
http://iserveandroid.blogspot.in/2010/11/how-to-dismiss-custom-dialog-based-on.html
您还在特定时间后关闭了对话框。见下面的链接。
http://xjaphx.wordpress.com/2011/07/13/auto-close-dialog-after-a-specific-time/
答案 3 :(得分:0)
如果您希望通过单击对话框并在对话框外部关闭自定义对话框,请使用以下代码:
private void showDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = (LayoutInflater)getSystemService (Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.your_custom_dialogLayout, null);
builder.setView(v);
builder.setCancelable(true);//Dialog dismissed by click outside
final AlertDialog dialog = builder.create();
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss(); //Dialog dismissed by click on dialogs content
}
});
dialog.show();
}