当我的 OptionsMenuItems 之一被点击时,我试图弹出一个警告对话框。如果用户单击“是”则继续操作,如果单击“否”则取消。我只是不知道如何编写代码。这就是我所拥有的:
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId()) {
case R.id.exit:
this.finish();
return true;
case R.id.about:
Intent i = new Intent(this, AboutActivity.class);
this.startActivity(i);
case R.id.skip:
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
// set the message to display
alertbox.setMessage("The yoga pose will be skipped from now on!").show();
alertbox.setCancelable(true);
alertbox.setNegativeButton("no").setPositiveButton("OK", new DialogClicklistener() {
// click listener on the alert box
public boolean onClick(DialogInterface arg0, int arg1) {
// the button was clicked
boolean success = myDbHelper.setSkip(PoseID);
SetImageView2(myDbHelper);
return success;
}
});
// add a neutral button to the alert box and assign a click listener
alertbox.setCancelable(true).set(onCancelListener) {
// click listener on the alert box
public boolean onClick(DialogInterface arg0, int arg1) {
// the button was clicked
}
});
// show it
alertbox.show();
default:
return super.onOptionsItemSelected(item);
}
}