我试图弹出一个警告对话框,当点击我的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);
}
}
以下是直接从我目前工作的应用程序。 它带来了一个AlertDialog,然后将用户带到一个不同的活动(在他们输入密码)。 请让我知道,如果我可以澄清这事。
编辑:整个方法现在包括在内。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
final int menuItem = item.getItemId();
if ((menuItem == R.id.survey) || (menuItem == R.id.syncMode)) {
View layout = inflater.inflate(R.layout.survey_password_dialog, (ViewGroup) findViewById(R.id.layout_root));
final EditText password = (EditText) layout.findViewById(R.id.passwordText);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(layout)
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
final AlertDialog alertDialog = builder.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
Button b = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (password.getText().toString().equalsIgnoreCase("the_password")) {
if (menuItem == R.id.survey) {
Intent intent = new Intent(AsthmaAppActivity.this, SurveyActivity.class);
startActivity(intent);
alertDialog.dismiss();
}
else { //(menuItem == R.id.syncMode)
startActivity(new Intent(AsthmaAppActivity.this, SyncMode.class));
alertDialog.dismiss();
}
}
else Toast.makeText(AsthmaAppActivity.this, "Password incorrect", Toast.LENGTH_LONG).show();
}
});
}
});
alertDialog.getWindow().setSoftInputMode (WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
alertDialog.show();
}
else { //dialog for setting application parameters "on the fly" for application testing
View layout = inflater.inflate(R.layout.parameter_change, (ViewGroup) findViewById(R.id.layout_root));
final EditText parameter = (EditText) layout.findViewById(R.id.parameterText);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(layout)
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String parameterString = parameter.getText().toString();
if(parameterString == null || parameterString.isEmpty()) {
testParam = 0.0;
}
else {
testParam = Double.parseDouble(parameterString);
}
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
})
.show();
}
return(super.onOptionsItemSelected(item));
}
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
alertbox.setPositiveButton("Yes" new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
log("i'm clicked");
}
});