Android的对话框,保持对话框打开时,pressed按钮? [英] Android Dialog, keep dialog open when button is pressed?
问题描述
我想继续我的对话框打开的时候我preSS的按钮。 目前,它的收盘!请帮帮忙!
AlertDialog.Builder建设者=新AlertDialog.Builder(本);
builder.setMessage(你确定要退出吗?)
.setCancelable(假)
.setPositiveButton(是,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话框,INT ID){
MyActivity.this.finish();
}
})
.setNegativeButton(否,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话框,INT ID){
dialog.cancel();
}
});
AlertDialog警报= builder.create();
是的,你可以。你基本上需要:
- 创建与DialogBuilder对话框
- 显示()对话框
- 找到在对话框中显示的按钮和覆盖其onClickListener
因此,创建一个监听器类:
类CustomListener实现View.OnClickListener {
私人最终对话框对话框;
公共CustomListener(对话对话框){
this.dialog =对话框;
}
@覆盖
公共无效的onClick(视图v){
//做你想做这里
//如果土特产品要关闭对话框,取消下面的行
//dialog.dismiss();
}
}
然后显示在对话框使用的时候:
AlertDialog对话框= dialogBuilder.create();
dialog.show();
按钮theButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
theButton.setOnClickListener(新CustomListener(对话));
记住,你需要显示的对话框,否则按钮不会findeable。此外,一定要修改 DialogInterface.BUTTON_POSITIVE 以任何价值,你用来添加按钮。另外请注意,添加的按钮DialogBuilder当你将需要提供onClickListeners - 你不能添加自定义监听器在那里,虽然 - 对话仍然会解散,如果你不重写后演出的听众()被调用。
I would like to keep my dialog open when I press a button. At the moment it's closing! Please help!
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MyActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
Yes, you can. You basically need to:
- Create the dialog with DialogBuilder
- show() the dialog
- Find the buttons in the dialog shown and override their onClickListener
So, create a listener class:
class CustomListener implements View.OnClickListener {
private final Dialog dialog;
public CustomListener(Dialog dialog) {
this.dialog = dialog;
}
@Override
public void onClick(View v) {
// Do whatever you want here
// If tou want to close the dialog, uncomment the line below
//dialog.dismiss();
}
}
Then when showing the dialog use:
AlertDialog dialog = dialogBuilder.create();
dialog.show();
Button theButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
theButton.setOnClickListener(new CustomListener(dialog));
Remember, you need to show the dialog otherwise the button will not be findeable. Also, be sure to change DialogInterface.BUTTON_POSITIVE to whatever value you used to add the button. Also note that when adding the buttons in the DialogBuilder you will need to provide onClickListeners - you cannot add the custom listener in there, though - the dialog will still dismiss if you do not override the listeners after show() is called.
这篇关于Android的对话框,保持对话框打开时,pressed按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!