我使用以下代碼來結束一個activity的運行。但是我想使用BackPress事件來結束list activity。這個方法怎麼實現,能在我原代碼上改嗎,請求大家幫忙!
public void onBackPressed() {
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setMessage("Want to exit ?");
alertDialog.setButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
for(Activity activity:activities)
activity.finish();
} });
alertDialog.setButton2("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}});
alertDialog.show();
}
我沒有改你的代碼,這是我的代碼,請你參考:
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode==KeyEvent.KEYCODE_BACK){
new AlertDialog.Builder(ProgressController.this).setTitle(
"Attention").setMessage(
"Want to exit ?")
.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
finish();
}
}).setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
}
}).show();
return true;
}else{
return false;
}
//return super.onKeyDown(keyCode, event);
}