如何在Android上显示警告对话框?

我想显示一个对话框/popup窗口,显示一条消息给用户,显示“你确定要删除这个条目吗? 用一个button说'删除'。 当Delete被触摸,它应该删除该条目,否则没有。

我已经为这些button写了一个点击监听器,但是如何调用一个对话框或popup框及其function?

您可以使用这个警报生成器:

  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("Delete entry") .setMessage("Are you sure you want to delete this entry?") .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // continue with delete } }) .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // do nothing } }) .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(); } } }); 

只是一个简单的! 创build一个对话框方法,类似于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(); } 

现在创build布局XML dialog_demo.xml并创build你的UI /devise。 以下是我为演示而创build的示例:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/dialog_info" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:text="@string/dialog_text"/> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:layout_below="@id/dialog_info"> <Button android:id="@+id/dialog_cancel" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="0.50" android:background="@color/dialog_cancel_bgcolor" android:text="Cancel"/> <Button android:id="@+id/dialog_ok" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="0.50" android:background="@color/dialog_ok_bgcolor" android:text="Agree"/> </LinearLayout> </RelativeLayout> 

现在你可以在任何你喜欢的地方调用openDialog()这里是上面代码的截图。

在这里输入图像说明

请注意,文本和颜色是从strings.xmlcolors.xml 。 你可以定义你自己的。

现在最好使用DialogFragment而不是直接创buildAlertDialog。

  • 怎么样? 请参阅: 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(); 
 // 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(); } 

这是如何创build警报对话框的基本示例:

 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(); 

在这里输入图像描述

我创build了一个询问Person的对话框,他是否打电话给Person。

 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(); } } 

为了我

 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(); 

这绝对有助于你。 试试这个代码:点击一个button,你可以把一个,两个或三个button,一个警告对话框…

 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(); } }); 

你可以试试这个….

  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(); 

欲了解更多信息,请查看此链接…

当你想closures对话框时要小心 – 使用dialog.dismiss() 。 在我第一次尝试使用dismissDialog(0) (我可能从某个地方复制) 有时工作。 系统提供的对象听起来像是一个更安全的select。

您也可以尝试这种方式,它会为您提供素材风格的对话框

 private void showDialog() { String text2 = "<font color=#212121>Medi Notification</font>";//for custom title color AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AppCompatAlertDialogStyle); builder.setTitle(Html.fromHtml(text2)); String text3 = "<font color=#A4A4A4>You can complete your profile now or start using the app and come back later</font>";//for custom message builder.setMessage(Html.fromHtml(text3)); builder.setPositiveButton("DELETE", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { toast = Toast.makeText(getApplicationContext(), "DELETE", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); } }); builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { toast = Toast.makeText(getApplicationContext(), "CANCEL", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); } }); builder.show(); } 
 public void showSimpleDialog(View view) { // Use the Builder class for convenient dialog construction AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setCancelable(false); builder.setTitle("AlertDialog Title"); builder.setMessage("Simple Dialog Message"); builder.setPositiveButton("OK!!!", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { // } }) .setNegativeButton("Cancel ", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); // Create the AlertDialog object and return it builder.create().show(); } 

在Android的对话框中查看我的博客,你会在这里find所有的细节: http : //www.fahmapps.com/2016/09/26/dialogs-in-android-part1/ 。

 import android.app.*; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.*; public class ShowPopUp extends Activity { PopupWindow popUp; LinearLayout layout; TextView tv; LayoutParams params; LinearLayout mainLayout; Button but; boolean click = true; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); popUp = new PopupWindow(this); layout = new LinearLayout(this); mainLayout = new LinearLayout(this); tv = new TextView(this); but = new Button(this); but.setText("Click Me"); but.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (click) { popUp.showAtLocation(mainLayout, Gravity.BOTTOM, 10, 10); popUp.update(50, 50, 300, 80); click = false; } else { popUp.dismiss(); click = true; } } }); params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); layout.setOrientation(LinearLayout.VERTICAL); tv.setText("Hi this is a sample text for popup window"); layout.addView(tv, params); popUp.setContentView(layout); // popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10); mainLayout.addView(but, params); setContentView(mainLayout); } }