对话框界面中的Android back软键代码行
如何在对话框界面中的Android back软键代码行,android,Android,如何在对话框界面中执行back命令。OnClickListner正按钮?我想首先询问用户是否希望返回到上一个活动。我试过super.onBackPressed()但它不是此的有效代码。 这是我的密码: @Override public void onBackPressed(){ DialogInterface.OnClickListener confirm = new DialogInterface.OnClickListener() { @Override
对话框界面中执行back命令。OnClickListner
正按钮?我想首先询问用户是否希望返回到上一个活动。我试过super.onBackPressed()代码>但它不是此的有效代码。
这是我的密码:
@Override
public void onBackPressed(){
DialogInterface.OnClickListener confirm = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
switch(which){
case DialogInterface.BUTTON_POSITIVE:{
//execute back function
super.onBackPressed();
break;
}
case DialogInterface.BUTTON_NEGATIVE:{
//Do nothing, just stay on the current activity
}
}
}
};
AlertDialog.Builder msgbox = new AlertDialog.Builder(this);
msgbox.setMessage("Are you sure you want to go back?");
msgbox.setPositiveButton("Yes", confirm);
msgbox.setNegativeButton("No", confirm);
msgbox.show();
}
super.onBackPressed()
给我这个错误提示类型对象的onBackPressed()方法未定义
好的,我已经对代码进行了更改以反映您的需要。这应该可以
@Override
public void onBackPressed(){
DialogInterface.OnClickListener confirm = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
switch(which){
case DialogInterface.BUTTON_POSITIVE:{
//execute back function
finish();
break;
}
case DialogInterface.BUTTON_NEGATIVE:{
//Do nothing, just stay on the current activity
break;
}
}
}
};
AlertDialog.Builder msgbox = new AlertDialog.Builder(this);
msgbox.setMessage("Are you sure you want to go back?");
msgbox.setPositiveButton("Yes", confirm);
msgbox.setNegativeButton("No", confirm);
msgbox.show();
}
我是否仍要在您给出的代码之上使用@Override public void onBackPressed(){}
@tgono,删除@Override public void onBackPressed(){}
并将我放置的覆盖放在处理按钮单击的代码上方的onBackPressed()中代码>本身。我还可以把我的对话框界面放在哪里。OnClickListener
?@EjDelRosario,它现在可以工作了吗?我编辑的answergetActivity()是一个错误。。我尝试用this.finish()替换它代码>仍然是一个错误。。如何正确地调用当前活动?