如何在DialogInterface.OnClickListner
肯定按钮内执行back命令?我想先询问用户是否要返回上一个活动。我已经尝试过了,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();
给了我这个错误提示The method onBackPressed() is undefined for the type object
好的,我已经对代码进行了更改以反映您的需求。这应该做。
@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();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句