Android中的AlertDialog使用示例一(警告对话框)
2016-11-23 11:43
483 查看
在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择。这些功能我们叫它Android Dialog对AlertDialog实现方法为建造者模式。下面我们模拟卸载应用程序时弹出的最为普通的警告对话框,如下图:
![](https://img-blog.csdn.net/20161123114317064?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
layout布局界面代码示例(大家可以自己扩展下,比如加入图片等):
layout布局界面代码示例(大家可以自己扩展下,比如加入图片等):
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:text="卸载" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="show" android:id="@+id/button" /> </LinearLayout>Java实现代码:
public class AlertDialogDemo extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.alterdialog); } public void show(View v){ //实例化建造者 AlertDialog.Builder builder = new AlertDialog.Builder(this); //设置警告对话框的标题 builder.setTitle("卸载"); //设置警告显示的图片 // builder.setIcon(android.R.drawable.ic_dialog_alert); //设置警告对话框的提示信息 builder.setMessage("确定卸载吗"); //设置”正面”按钮,及点击事件 builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(AlertDialogDemo.this,"点击了确定按钮",Toast.LENGTH_SHORT).show(); } }); //设置“反面”按钮,及点击事件 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(AlertDialogDemo.this,"点击了取消按钮",Toast.LENGTH_SHORT).show(); } }); //设置“中立”按钮,及点击事件 builder.setNeutralButton("等等看吧", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(AlertDialogDemo.this,"点击了中立按钮",Toast.LENGTH_SHORT).show(); } }); //显示对话框 builder.show(); } }
相关文章推荐
- Android中的AlertDialog使用示例一(警告对话框)
- Android中的AlertDialog使用示例二(普通选项对话框)
- Android中的AlertDialog使用示例二(普通选项对话框)
- Android中的AlertDialog使用示例三(单向选择确定对话框)
- Android中的AlertDialog使用示例四(多项选择确定对话框)
- Android中的AlertDialog使用示例五(自定义对话框)
- Android中的AlertDialog使用示例五(自定义对话框)
- Android中的警告对话框(AlertDialog)使用案例
- 【Android开发】消息提示框与对话框-使用AlertDialog创建对话框
- Android 对话框AlertDialog,PopupWindow使用大全
- ANDROID 系统提示对话框(ALERTDIALOG)的使用
- android Dialog对话框使用示例
- Android AlertDialog对话框的使用及实例(显示提示消息的对话框)
- 【Android】Android中AlertDialog对话框的使用实例
- Android中AlertDialog对话框的使用
- android 系统提示对话框(AlertDialog)的使用
- Android之警告对话框alertdialog
- android Dialog对话框使用示例
- Android开发之使用AlertDialog创建对话框,单选框和多选框
- 【Android】6.2 AlertDialog(警告对话框)