当前位置:K88软件开发文章中心编程语言APP编程Android01 → 文章内容

2.5.9 AlertDialog(对话框)详解

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-12 6:25:47

本节引言:本节继续给大家带来是显示提示信息的第三个控件AlertDialog(对话框),同时它也是其他Dialog的的父类!比如ProgressDialog,TimePickerDialog等,而AlertDialog的父类是:Dialog!另外,不像前面学习的Toast和Notification,AlertDialog并不能直接new出来,如果你打开AlertDialog的源码,会发现构造方法是protected的,如果我们要创建AlertDialog的话,我们需要使用到该类中的一个静态内部类:public static class Builder,然后来调用AlertDialog里的相关方法,来对AlertDialog进行定制,最后调用show()方法来显示我们的AlertDialog对话框!好的,下面我们就来学习AlertDialog的基本用法,以及定制我们的AlertDialog!官方文档:AlertDialog1.基本使用流程Step 1:创建AlertDialog.Builder对象;Step 2:调用setIcon()设置图标,setTitle()或setCustomTitle()设置标题;Step 3:设置对话框的内容:setMessage()还有其他方法来指定显示的内容;Step 4:调用setPositive/Negative/NeutralButton()设置:确定,取消,中立按钮;Step 5:调用create()方法创建这个对象,再调用show()方法将对话框显示出来;2.几种常用的对话框使用示例运行效果图:核心代码:MainActivity.java:public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button btn_dialog_one; private Button btn_dialog_two; private Button btn_dialog_three; private Button btn_dialog_four; private Context mContext; private boolean[] checkItems; private AlertDialog alert = null; private AlertDialog.Builder builder = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = MainActivity.this; bindView(); } private void bindView() { btn_dialog_one = (Button) findViewById(R.id.btn_dialog_one); btn_dialog_two = (Button) findViewById(R.id.btn_dialog_two); btn_dialog_three = (Button) findViewById(R.id.btn_dialog_three); btn_dialog_four = (Button) findViewById(R.id.btn_dialog_four); btn_dialog_one.setOnClickListener(this); btn_dialog_two.setOnClickListener(this); btn_dialog_three.setOnClickListener(this); btn_dialog_four.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { //普通对话框 case R.id.btn_dialog_one: alert = null; builder = new AlertDialog.Builder(mContext); alert = builder.setIcon(R.mipmap.ic_icon_fish) .setTitle("系统提示:") .setMessage("这是一个最普通的AlertDialog,\n带有三个按钮,分别是取消,中立和确定") .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(mContext, "你点击了取消按钮~", Toast.LENGTH_SHORT).show(); } }) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(mContext, "你点击了确定按钮~", Toast.LENGTH_SHORT).show(); } }) .setNeutralButton("中立", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(mContext, "你点击了中立按钮~", Toast.LENGTH_SHORT).show(); } }).create(); //创建AlertDialog对象 alert.show(); //显示对话框 break; //普通列表对话框 case R.id.btn_dialog_two: final String[] lesson = new String[]{"语文", "数学", "英语", "化学", "生物", "物理", "体育"}; alert = null; builder = new AlertDialog.Builder(mContext); alert = builder.setIcon(R.mipmap.ic_icon_fish) .setTitle("选择你喜欢的课程") .setItems(lesson, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "你选择了" + lesson[which], Toast.LENGTH_SHORT).show(); } }).create(); alert.show(); break; //单选列表对话框 case R.id.btn_dialog_three: final String[] fruits = new String[]{"苹果", "雪梨", "香蕉", "葡萄", "西瓜"}; alert = null; builder = new AlertDialog.Builder(mContext); alert = builder.setIcon(R.mipmap.ic_icon_fish) .setTitle("选择你喜欢的水果,只能选一个哦~")

[1] [2] [3]  下一页


2.5.9 AlertDialog(对话框)详解