[英]AlertDialog does not show onclick
AlertDialog不显示onclick。
长按一个按钮时,应该会弹出一个对话框,但没有。 可以访问longClicklistener,但仍然不会弹出对话框。
// Set Reset Button
resetBtn = (ImageButton) findViewById(R.id.resetButton);
resetBtn.setOnLongClickListener(new Button.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
AlertDialogWrapper.Builder alertDialog = new AlertDialogWrapper.Builder(MainActivity.this);
alertDialog.setPositiveButton(getString(R.string.MAIN_MENU_RESET_ALERT_YES), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke YES event
Toast toast = Toast.makeText(MainActivity.this, getString(R.string.MAIN_MENU_RESET_ALERT_RESULT_YES), Toast.LENGTH_LONG);
toast.show();
}
}); alertDialog.setNegativeButton(getString(R.string.MAIN_MENU_RESET_ALERT_NO), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, getString(R.string.MAIN_MENU_RESET_ALERT_RESULT_NO), Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
// Showing Alert Message
return true;
}
});
你忘了
alertDialog.show();
您缺少alertDialog.show()
// Set Reset Button
resetBtn = (ImageButton) findViewById(R.id.resetButton);
resetBtn.setOnLongClickListener(new Button.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
AlertDialogWrapper.Builder alertDialog = new AlertDialogWrapper.Builder(MainActivity.this);
alertDialog.setPositiveButton(getString(R.string.MAIN_MENU_RESET_ALERT_YES), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke YES event
Toast toast = Toast.makeText(MainActivity.this, getString(R.string.MAIN_MENU_RESET_ALERT_RESULT_YES), Toast.LENGTH_LONG);
toast.show();
}
});
alertDialog.setNegativeButton(getString(R.string.MAIN_MENU_RESET_ALERT_NO), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, getString(R.string.MAIN_MENU_RESET_ALERT_RESULT_NO), Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
/*** Showing Alert Message ***/
alertDialog.show();
return true;
}
});
// Set Reset Button
resetBtn = (ImageButton) findViewById(R.id.resetButton);
resetBtn.setOnLongClickListener(new Button.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
AlertDialogWrapper.Builder alertDialog = new AlertDialogWrapper.Builder(MainActivity.this);
alertDialog.setPositiveButton(getString(R.string.MAIN_MENU_RESET_ALERT_YES), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke YES event
Toast toast = Toast.makeText(MainActivity.this, getString(R.string.MAIN_MENU_RESET_ALERT_RESULT_YES), Toast.LENGTH_LONG);
toast.show();
}
}); alertDialog.setNegativeButton(getString(R.string.MAIN_MENU_RESET_ALERT_NO), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, getString(R.string.MAIN_MENU_RESET_ALERT_RESULT_NO), Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
alertDialog.show();
// Showing Alert Message
return true;
}
});
您需要致电show()
只需添加
alertDialog.show();
return true
之前。
那是,
// Showing Alert Message
alertDialog.show(); // code to show alert message [Missed]
return true;
您错过了显示对话框的代码中的主线...只需在行下方添加
// Showing Alert Message
alertDialog.show();
return true;
将您的AlertDialogWrapper.Builder
更改为AlertDialog.Builder
。 并且在return true;
之前return true;
调用alertDialog.create();
和alertDialog.show()
。
您缺少alertDialog.show()并在长时间单击时看到警报对话框的示例
SingleButtton.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// Creating alert Dialog with one Button
AlertDialog alertDialog = new
AlertDialog.Builder(AlertDialogActivity.this).create();
// Setting Dialog Title
alertDialog.setTitle("Alert Dialog");
// Setting Dialog Message
alertDialog.setMessage("Welcome to Android Application");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.tick);
// Setting OK Button
alertDialog.setButton("OK", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which)
{
// Write your code here to execute after dialog closed
Toast.makeText(getApplicationContext(),"You clicked on OK", Toast.LENGTH_SHORT).show();
}
});
// Showing Alert Message
alertDialog.show();
return true;
}
});
btnAlertTwoBtns.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// Creating alert Dialog with two Buttons
AlertDialog.Builder alertDialog = new AlertDialog.Builder(AlertDialogActivity.this);
// Setting Dialog Title
alertDialog.setTitle("Confirm Delete...");
// Setting Dialog Message
alertDialog.setMessage("Are you sure you want delete this?");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.delete);
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
// Write your code here to execute after dialog
Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog
Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
return true;
}
});
btnAlertThreeBtns.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// Creating alert Dialog with three Buttons
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
AlertDialogActivity.this);
// Setting Dialog Title
alertDialog.setTitle("Save File...");
// Setting Dialog Message
alertDialog.setMessage("Do you want to save this file?");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.save);
// Setting Positive Yes Button
alertDialog.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// User pressed Cancel button. Write Logic Here
Toast.makeText(getApplicationContext(),
"You clicked on YES",
Toast.LENGTH_SHORT).show();
}
});
// Setting Negative No Button... Neutral means in between yes and cancel button
alertDialog.setNeutralButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// User pressed No button. Write Logic Here
Toast.makeText(getApplicationContext(),
"You clicked on NO", Toast.LENGTH_SHORT)
.show();
}
});
// Setting Positive "Cancel" Button
alertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// User pressed Cancel button. Write Logic Here
Toast.makeText(getApplicationContext(),
"You clicked on Cancel",
Toast.LENGTH_SHORT).show();
}
});
// Showing Alert Message
alertDialog.show();
return true;
}
});
You forget to do this......
AlertDialog aD = alertDialog.create();
try {
aD.show();
} catch (Exception e) {
e.printStackTrace();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.