• Android对话框(Dialog)


    Android对话框

    前几天出差没有进行更新,今天写一下安卓中用的比较多的对话框——AlertDialog。

    dialog就是一个在屏幕上弹出一个可以让用户做出一个选择,或者输入额外的信息的对话框,一个对话框并不会沾满我们整个的屏幕,并且通常用于模型事件当中需要用户做出一个决定后才会继续执行。

    对于对话框,我们主要用三种,分别是普通对话框、单选对话框、多选对话框。

    效果图

    对话框没什么难点,直接上代码

      1 public class MainActivity extends AppCompatActivity implements View.OnClickListener {
      2 
      3     private Button btn_dialog_one;
      4     private Button btn_dialog_three;
      5     private Button btn_dialog_four;
      6 
      7     private Context mContext;
      8     private boolean[] checkItems;
      9 
     10     private AlertDialog alert = null;
     11     private AlertDialog.Builder builder = null;
     12 
     13     @Override
     14     protected void onCreate(Bundle savedInstanceState) {
     15         super.onCreate(savedInstanceState);
     16         setContentView(R.layout.activity_main);
     17         mContext = MainActivity.this;
     18         bindView();
     19 
     20 
     21     }
     22 
     23     private void bindView() {
     24         btn_dialog_one = (Button) findViewById(R.id.btn_dialog_one);
     25         btn_dialog_three = (Button) findViewById(R.id.btn_dialog_three);
     26         btn_dialog_four = (Button) findViewById(R.id.btn_dialog_four);
     27         btn_dialog_one.setOnClickListener(this);
     28         btn_dialog_three.setOnClickListener(this);
     29         btn_dialog_four.setOnClickListener(this);
     30     }
     31 
     32 
     33     @Override
     34     public void onClick(View v) {
     35         switch (v.getId()) {
     36             //普通对话框
     37             case R.id.btn_dialog_one:
     38                 alert = null;
     39                 builder = new AlertDialog.Builder(mContext);
     40                 alert = builder.setIcon(R.mipmap.ic_icon_fish)
     41                         .setTitle("系统提示:")
     42                         .setMessage("这是一个最普通的AlertDialog,
    带有三个按钮,分别是取消,中立和确定")
     43                         .setNegativeButton("取消", new DialogInterface.OnClickListener() {
     44                             @Override
     45                             public void onClick(DialogInterface dialog, int which) {
     46                                 Toast.makeText(mContext, "你点击了取消按钮~", Toast.LENGTH_SHORT).show();
     47                             }
     48                         })
     49                         .setPositiveButton("确定", new DialogInterface.OnClickListener() {
     50                             @Override
     51                             public void onClick(DialogInterface dialog, int which) {
     52                                 Toast.makeText(mContext, "你点击了确定按钮~", Toast.LENGTH_SHORT).show();
     53                             }
     54                         })
     55                         .setNeutralButton("中立", new DialogInterface.OnClickListener() {
     56                             @Override
     57                             public void onClick(DialogInterface dialog, int which) {
     58                                 Toast.makeText(mContext, "你点击了中立按钮~", Toast.LENGTH_SHORT).show();
     59                             }
     60                         }).create();             //创建AlertDialog对象
     61                 alert.show();                    //显示对话框
     62                 break;
     63             //单选列表对话框
     64             case R.id.btn_dialog_three:
     65                 final String[] fruits = new String[]{"苹果", "雪梨", "香蕉", "葡萄", "西瓜"};
     66                 alert = null;
     67                 builder = new AlertDialog.Builder(mContext);
     68                 alert = builder.setIcon(R.mipmap.ic_icon_fish)
     69                         .setTitle("选择你喜欢的水果,只能选一个哦~")
     70                         .setSingleChoiceItems(fruits, 0, new DialogInterface.OnClickListener() {
     71                             @Override
     72                             public void onClick(DialogInterface dialog, int which) {
     73                                 Toast.makeText(getApplicationContext(), "你选择了" + fruits[which], Toast.LENGTH_SHORT).show();
     74                             }
     75                         }).create();
     76                 alert.show();
     77                 break;
     78             //多选列表对话框
     79             case R.id.btn_dialog_four:
     80                 final String[] menu = new String[]{"水煮豆腐", "萝卜牛腩", "酱油鸡", "胡椒猪肚鸡"};
     81                 //定义一个用来记录个列表项状态的boolean数组
     82                 checkItems = new boolean[]{false, false, false, false};
     83                 alert = null;
     84                 builder = new AlertDialog.Builder(mContext);
     85                 alert = builder.setIcon(R.mipmap.ic_icon_fish)
     86                         .setMultiChoiceItems(menu, checkItems, new DialogInterface.OnMultiChoiceClickListener() {
     87                             @Override
     88                             public void onClick(DialogInterface dialog, int which, boolean isChecked) {
     89                                 checkItems[which] = isChecked;
     90                             }
     91                         })
     92                         .setPositiveButton("确定", new DialogInterface.OnClickListener() {
     93                             @Override
     94                             public void onClick(DialogInterface dialog, int which) {
     95                                 String result = "";
     96                                 for (int i = 0; i < checkItems.length; i++) {
     97                                     if (checkItems[i])
     98                                         result += menu[i] + " ";
     99                                 }
    100                                 Toast.makeText(getApplicationContext(), "客官你点了:" + result, Toast.LENGTH_SHORT).show();
    101                             }
    102                         })
    103                         .create();
    104                 alert.show();
    105                 break;
    106         }
    107     }
    108 }

    布局是界面中加上三个按钮,就不往上贴了。。。。。。

    下面是对话框的实现步骤

    1. 创建AlertDialog.Builder对象;
    2. 调用setIcon()设置图标,setTitle()setCustomTitle()设置标题;
    3. 设置对话框的内容:setMessage()还有其他方法来指定显示的内容;
    4. 调用setPositive/Negative/NeutralButton()设置:确定,取消,中立按钮;
    5. 调用create()方法创建这个对象,再调用show()方法将对话框显示出来;
  • 相关阅读:
    1029 旧键盘 (20 分)C、Java、python
    1028 人口普查 (20 分)C语言
    1027 打印沙漏 (20 分)C语言
    luoguP2709 小B的询问
    luoguP2709 小B的询问
    bzoj5016 [Snoi2017]一个简单的询问
    bzoj5016 [Snoi2017]一个简单的询问
    luoguP1972 [SDOI2009]HH的项链(莫队)
    luoguP1972 [SDOI2009]HH的项链(莫队)
    Tyvj1091
  • 原文地址:https://www.cnblogs.com/QY-admin/p/10012522.html
Copyright © 2020-2023  润新知