您好我一直在通过单击已打开的对话框上的按钮来创建对话框
第一个对话框的名称= dialog_main
第二个对话框的名称= dialog_edit
首先,我打开dialog_main。它上面有一个按钮,按下后会打开dialog_edit。
当我点击dialog_edit外部时,它会返回到dialog_main,这就是它应该做的事情
但是当我点击后退按钮时,它会同时取消两个对话框并返回主要活动。
我想要的是让dialog_edit的后退按钮事件返回到dialog_main。请参阅代码。
final Dialog dialog_main = new Dialog(ViewTask.this);
dialog_main.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog_main.setContentView(R.layout.dialog_main);
dialog_main.setCanceledOnTouchOutside(true);
dialog_main.setCancelable(true);
dialog_main.show();
//button is a button displayed on dialog_main
button_edit_details.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog_edit = new Dialog(ViewTask.this);
dialog_edit.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog_edit.setContentView(R.layout.dialog_edit_details);
dialog_edit.setCancelable(true);
dialog_edit.setCanceledOnTouchOutside(true);
dialog_edit.show();
dialog_edit.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {
//do the operations
dialog_edit.dismiss();
});
dialog_edit.setOnKeyListener(new Dialog.OnKeyListener() {
@Override
public boolean onKey(DialogInterface arg0, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//do same operations
dialog_edit.dismiss();
});
}
}); //end of button onclicklistener
dialog_main.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog_main) {
//Data is displayed from operations in dialog_edit
dialog_main.dismiss();
}
});
dialog_main.setOnKeyListener(new Dialog.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog_main, int keyCode2, KeyEvent event2) {
if (keyCode2 == KeyEvent.KEYCODE_BACK) {
//Data is displayed from operations in dialog_edit
dialog_main.dismiss();
}
return true;
}
});
}
代码中可能存在一些错误,但我希望您能了解我在做什么。我尝试了一些在其他地方放置“dialog_main.dissmiss()”的组合,但问题仍然存在。
答案 0 :(得分:1)
我能想到的一个解决方案是在适当的位置切换主对话框的可取消属性。
final Dialog dialog_main = new Dialog(ViewTask.this);
dialog_main.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog_main.setContentView(R.layout.dialog_main);
dialog_main.setCanceledOnTouchOutside(true);
dialog_main.setCancelable(true);
dialog_main.show();
//button is a button displayed on dialog_main
button_edit_details.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog_edit = new Dialog(ViewTask.this);
dialog_edit.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog_edit.setContentView(R.layout.dialog_edit_details);
dialog_edit.setCancelable(true);
dialog_edit.setCanceledOnTouchOutside(true);
//mark main dialog as non-cancelable here
dialog_main.setCancelable(false);
dialog_edit.show();
dialog_edit.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {
//do the operations
dialog_edit.dismiss();
});
dialog_edit.setOnKeyListener(new Dialog.OnKeyListener() {
@Override
public boolean onKey(DialogInterface arg0, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//do same operations
dialog_edit.dismiss();
//mark main dialog as cancelable again
dialog_main.setCancelable(true);
});
}
}); //end of button onclicklistener
dialog_main.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog_main) {
//Data is displayed from operations in dialog_edit
dialog_main.dismiss();
}
});
dialog_main.setOnKeyListener(new Dialog.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog_main, int keyCode2, KeyEvent event2) {
if (keyCode2 == KeyEvent.KEYCODE_BACK) {
//Data is displayed from operations in dialog_edit
dialog_main.dismiss();
}
return true;
}
});
}
答案 1 :(得分:0)
我还没有测试过,但我想这会解决你的问题..! 替换以下代码..!
dialog_main.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog_main.setContentView(R.layout.dialog_main);
dialog_main.setCanceledOnTouchOutside(false);
dialog_main.setCancelable(false);
dialog_main.show();