我想显示一个对话框/弹出窗口,其中显示一条消息,显示"您确定要删除此条目吗?" 用一个按钮说"删除".当Delete
被触摸时,应该删除该条目,否则什么都没有.
我为这些按钮编写了一个单击侦听器,但是如何调用对话框或弹出窗口及其功能?
您可以使用AlertDialog
for,并使用其Builder
类构造一个.下面的示例使用默认构造函数,该构造函数仅接受a,Context
因为对话框将从您传入的Context继承正确的主题,但是还有一个构造函数,允许您指定特定的主题资源作为第二个参数,如果您希望这样做所以.
new AlertDialog.Builder(context) .setTitle("Delete entry") .setMessage("Are you sure you want to delete this entry?") // Specifying a listener allows you to take an action before dismissing the dialog. // The dialog is automatically dismissed when a dialog button is clicked. .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // Continue with delete operation } }) // A null listener allows the button to dismiss the dialog and take no further action. .setNegativeButton(android.R.string.no, null) .setIcon(android.R.drawable.ic_dialog_alert) .show();
试试这段代码:
AlertDialog.Builder builder1 = new AlertDialog.Builder(context); builder1.setMessage("Write your message here."); builder1.setCancelable(true); builder1.setPositiveButton( "Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); builder1.setNegativeButton( "No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog alert11 = builder1.create(); alert11.show();
David Hedlund发布的代码给了我错误:
无法添加窗口 - 令牌null无效
如果您收到相同的错误,请使用以下代码.有用!!
runOnUiThread(new Runnable() { @Override public void run() { if (!isFinishing()){ new AlertDialog.Builder(YourActivity.this) .setTitle("Your Alert") .setMessage("Your Message") .setCancelable(false) .setPositiveButton("ok", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Whatever... } }).show(); } } });
只是一个简单的!在Java类中的任何位置创建一个对话框方法:
public void openDialog() { final Dialog dialog = new Dialog(context); // Context, this, etc. dialog.setContentView(R.layout.dialog_demo); dialog.setTitle(R.string.dialog_title); dialog.show(); }
现在创建Layout XML dialog_demo.xml
并创建UI /设计.以下是我为演示目的创建的示例:
现在你可以openDialog()
在任何你喜欢的地方打电话:)这是上面代码的截图.
请注意,文本和颜色使用strings.xml
和colors.xml
.你可以定义自己的.
现在最好使用DialogFragment而不是直接创建AlertDialog.
怎么样?请参阅:https://stackoverflow.com/a/21032871/1390874
为什么?请参阅:https://stackoverflow.com/a/13765411/1390874
您可以使用此代码:
AlertDialog.Builder alertDialog2 = new AlertDialog.Builder( AlertDialogActivity.this); // Setting Dialog Title alertDialog2.setTitle("Confirm Delete..."); // Setting Dialog Message alertDialog2.setMessage("Are you sure you want delete this file?"); // Setting Icon to Dialog alertDialog2.setIcon(R.drawable.delete); // Setting Positive "Yes" Btn alertDialog2.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" Btn alertDialog2.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 Dialog alertDialog2.show();
使用AlertDialog.Builder
AlertDialog alertDialog = new AlertDialog.Builder(this) //set icon .setIcon(android.R.drawable.ic_dialog_alert) //set title .setTitle("Are you sure to Exit") //set message .setMessage("Exiting will call finish() method") //set positive button .setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { //set what would happen when positive button is clicked finish(); } }) //set negative button .setNegativeButton("No", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { //set what should happen when negative button is clicked Toast.makeText(getApplicationContext(),"Nothing Happened",Toast.LENGTH_LONG).show(); } }) .show();
您将获得以下输出.
要查看警报对话框教程,请使用以下链接.
Android警报对话教程
为了我
new AlertDialog.Builder(this) .setTitle("Closing application") .setMessage("Are you sure you want to exit?") .setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).setNegativeButton("No", null).show();
// Dialog box public void dialogBox() { AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); alertDialogBuilder.setMessage("Click on Image for tag"); alertDialogBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { } }); alertDialogBuilder.setNegativeButton("cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { } }); AlertDialog alertDialog = alertDialogBuilder.create(); alertDialog.show(); }
new AlertDialog.Builder(context) .setTitle("title") .setMessage("message") .setPositiveButton(android.R.string.ok, null) .show();
这是如何创建警报对话框的基本示例:
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this); dialog.setCancelable(false); dialog.setTitle("Dialog on Android"); dialog.setMessage("Are you sure you want to delete this entry?" ); dialog.setPositiveButton("Delete", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { //Action for "Delete". } }) .setNegativeButton("Cancel ", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //Action for "Cancel". } }); final AlertDialog alert = dialog.create(); alert.show();
这绝对有助于你.试试这个代码:点击一个按钮,你可以把一个,两个或三个按钮带一个警告对话框......
SingleButtton.setOnClickListener(new View.OnClickListener() { public void onClick(View arg0) { // 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(); } }); btnAlertTwoBtns.setOnClickListener(new View.OnClickListener() { public void onClick(View arg0) { // 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(); } }); btnAlertThreeBtns.setOnClickListener(new View.OnClickListener() { public void onClick(View arg0) { // 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(); } });
我创建了一个对话框,询问一个人是否要打电话给一个人.
import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.Toast; public class Firstclass extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first); ImageView imageViewCall = (ImageView) findViewById(R.id.ring_mig); imageViewCall.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { try { showDialog("0728570527"); } catch (Exception e) { e.printStackTrace(); } } }); } public void showDialog(final String phone) throws Exception { AlertDialog.Builder builder = new AlertDialog.Builder(Firstclass.this); builder.setMessage("Ring: " + phone); builder.setPositiveButton("Ring", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent callIntent = new Intent(Intent.ACTION_DIAL);// (Intent.ACTION_CALL); callIntent.setData(Uri.parse("tel:" + phone)); startActivity(callIntent); dialog.dismiss(); } }); builder.setNegativeButton("Avbryt", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.show(); } }
你可以尝试这个....
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this); dialog.setCancelable(false); dialog.setTitle("Dialog on Android"); dialog.setMessage("Are you sure you want to delete this entry?" ); dialog.setPositiveButton("Delete", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { //Action for "Delete". } }) .setNegativeButton("Cancel ", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //Action for "Cancel". } }); final AlertDialog alert = dialog.create(); alert.show();
有关详细信息,请查看此链接...
您可以使用创建对话框 AlertDialog.Builder
试试这个:
AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("Are you sure you want to delete this entry?"); builder.setPositiveButton("Yes, please", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //perform any action Toast.makeText(getApplicationContext(), "Yes clicked", Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("No", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //perform any action Toast.makeText(getApplicationContext(), "No clicked", Toast.LENGTH_SHORT).show(); } }); //creating alert dialog AlertDialog alertDialog = builder.create(); alertDialog.show();
要更改"警报"对话框的正负按钮的颜色,可以在后面写下以下两行 alertDialog.show();
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.colorPrimary)); alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.colorPrimaryDark));
showDialog(MainActivity.this, "title", "message", "OK", "Cancel", {...}, {...});科特林
fun showDialog(context: Context, title: String, msg: String, positiveBtnText: String, negativeBtnText: String?, positiveBtnClickListener: DialogInterface.OnClickListener, negativeBtnClickListener: DialogInterface.OnClickListener?): AlertDialog { val builder = AlertDialog.Builder(context) .setTitle(title) .setMessage(msg) .setCancelable(true) .setPositiveButton(positiveBtnText, positiveBtnClickListener) if (negativeBtnText != null) builder.setNegativeButton(negativeBtnText, negativeBtnClickListener) val alert = builder.create() alert.show() return alert }爪哇
public static AlertDialog showDialog(@NonNull Context context, @NonNull String title, @NonNull String msg, @NonNull String positiveBtnText, @Nullable String negativeBtnText, @NonNull DialogInterface.OnClickListener positiveBtnClickListener, @Nullable DialogInterface.OnClickListener negativeBtnClickListener) { AlertDialog.Builder builder = new AlertDialog.Builder(context) .setTitle(title) .setMessage(msg) .setCancelable(true) .setPositiveButton(positiveBtnText, positiveBtnClickListener); if (negativeBtnText != null) builder.setNegativeButton(negativeBtnText, negativeBtnClickListener); AlertDialog alert = builder.create(); alert.show(); return alert; }
new AlertDialog.Builder(v.getContext()).setMessage("msg to display!").show();
当你想要关闭对话框时要小心 - 使用dialog.dismiss()
.在我第一次尝试时dismissDialog(0)
(我可能从某个地方复制过)有时会起作用.使用该对象系统提供的声音就像一个更安全的选择.
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this); // set title alertDialogBuilder.setTitle("AlertDialog Title"); // set dialog message alertDialogBuilder .setMessage("Some Alert Dialog message.") .setCancelable(false) .setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { Toast.makeText(this, "OK button click ", Toast.LENGTH_SHORT).show(); } }) .setNegativeButton("CANCEL",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { Toast.makeText(this, "CANCEL button click ", Toast.LENGTH_SHORT).show(); dialog.cancel(); } }); // create alert dialog AlertDialog alertDialog = alertDialogBuilder.create(); // show it alertDialog.show();
我想通过分享一个比他发布的更动态的方法来增加David Hedlund的好答案,这样当你有一个负面的动作来执行它时可以使用它,当你没有时,我希望它有所帮助.
private void showAlertDialog(@NonNull Context context, @NonNull String alertDialogTitle, @NonNull String alertDialogMessage, @NonNull String positiveButtonText, @Nullable String negativeButtonText, @NonNull final int positiveAction, @Nullable final Integer negativeAction, @NonNull boolean hasNegativeAction) { AlertDialog.Builder builder; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { builder = new AlertDialog.Builder(context, android.R.style.Theme_Material_Dialog_Alert); } else { builder = new AlertDialog.Builder(context); } builder.setTitle(alertDialogTitle) .setMessage(alertDialogMessage) .setPositiveButton(positiveButtonText, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { switch (positiveAction) { case 1: //TODO:Do your positive action here break; } } }); if(hasNegativeAction || negativeAction!=null || negativeButtonText!=null) { builder.setNegativeButton(negativeButtonText, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { switch (negativeAction) { case 1: //TODO:Do your negative action here break; //TODO: add cases when needed } } }); } builder.setIcon(android.R.drawable.ic_dialog_alert); builder.show(); }