Chinaunix首页 | 论坛 | 博客
  • 博客访问: 233466
  • 博文数量: 80
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 781
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-08 10:41
个人简介

爱莉清

文章分类

全部博文(80)

文章存档

2018年(1)

2017年(18)

2016年(49)

2015年(7)

2014年(5)

我的朋友

分类: Android平台

2016-09-09 14:47:39

使用对话框的时候有些点击了并不会自己消失需要自己添加.heid()来隐藏,这样用户体验会更好。

下面来看看效果:


1.基本使用流程

  • Step 1:创建AlertDialog.Builder对象;
  • Step 2:调用setIcon()设置图标,setTitle()setCustomTitle()设置标题;
  • Step 3:设置对话框的内容:setMessage()还有其他方法来指定显示的内容;
  • Step 4:调用setPositive/Negative/NeutralButton()设置:确定,取消,中立按钮;
  • Step 5:调用create()方法创建这个对象,再调用show()方法将对话框显示出来;

附上代码:
MainActivity.java

点击(此处)折叠或打开

  1. package nguhyw.alertdialog;

  2. import android.app.AlertDialog;
  3. import android.content.Context;
  4. import android.content.DialogInterface;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.Toast;

  10. public class MainActivity extends AppCompatActivity implements View.OnClickListener {

  11.     private Button btn_normal;
  12.     private Button btn_List1;
  13.     private Button btn_List2;
  14.     private Button btn_List3;

  15.     private Context mContext;
  16.     private boolean[] checkItems;

  17.     private AlertDialog alert = null;
  18.     private AlertDialog.Builder builder = null;

  19.     @Override
  20.     protected void onCreate(Bundle savedInstanceState) {
  21.         super.onCreate(savedInstanceState);
  22.         setContentView(R.layout.activity_main);

  23.         mContext = MainActivity.this;
  24.         binView();
  25.     }

  26.     public void binView(){
  27.         btn_normal = (Button)findViewById(R.id.btn_normal);
  28.         btn_List1 = (Button)findViewById(R.id.btn_List1);
  29.         btn_List2 = (Button)findViewById(R.id.btn_List2);
  30.         btn_List3 = (Button)findViewById(R.id.btn_List3);

  31.         btn_normal.setOnClickListener(this);
  32.         btn_List1.setOnClickListener(this);
  33.         btn_List2.setOnClickListener(this);
  34.         btn_List3.setOnClickListener(this);
  35.     }


  36.     @Override
  37.     public void onClick(View v) {

  38.         switch (v.getId()){
  39.             case R.id.btn_normal:
  40.                 show_normal_dialog();
  41.                 break;

  42.             case R.id.btn_List1:
  43.                 show_List1_dialog();
  44.                 break;

  45.             case R.id.btn_List2:
  46.                 show_List2_dialog();
  47.                 break;

  48.             case R.id.btn_List3:
  49.                 show_List3_dialog();
  50.                 break;

  51.             default:
  52.                 break;
  53.         }

  54.     }

  55.     public void show_normal_dialog(){

  56.         alert = null;
  57.         builder = new AlertDialog.Builder(mContext);
  58.         alert = builder.setIcon(R.drawable.nguhyw)
  59.                 .setTitle("系统提示")
  60.                 .setMessage("这是一个很普通的对话框,你想打我也没用!")
  61.                 .setNegativeButton("取消", new DialogInterface.OnClickListener() {
  62.                     @Override
  63.                     public void onClick(DialogInterface dialog, int which) {
  64.                         Toast.makeText(mContext,"你点击了取消按钮",Toast.LENGTH_SHORT).show();
  65.                     }
  66.                 })
  67.                 .setPositiveButton("确定", new DialogInterface.OnClickListener() {
  68.                     @Override
  69.                     public void onClick(DialogInterface dialog, int which) {
  70.                         Toast.makeText(mContext,"你点击了确定按钮",Toast.LENGTH_SHORT).show();
  71.                     }
  72.                 })
  73.                 .setNeutralButton("中立", new DialogInterface.OnClickListener() {
  74.                     @Override
  75.                     public void onClick(DialogInterface dialog, int which) {
  76.                         Toast.makeText(mContext, "你点击了中立按钮", Toast.LENGTH_SHORT).show();
  77.                     }
  78.                 })
  79.                 .create();
  80.         alert.show();
  81.     }

  82.     public void show_List1_dialog(){
  83.         final String[] lesson = new String[]{"语文","数学","英语","物理","化学","生物","历史",};
  84.         alert = null;
  85.         builder = new AlertDialog.Builder(mContext);
  86.         alert = builder.setIcon(R.drawable.nguhyw)
  87.                 .setTitle("选择你喜欢的课程")
  88.                 .setItems(lesson, new DialogInterface.OnClickListener() {
  89.                     @Override
  90.                     public void onClick(DialogInterface dialog, int which) {
  91.                         Toast.makeText(getApplicationContext(),"你选择了"+lesson[which],Toast.LENGTH_SHORT).show();
  92.                         alert.hide();
  93.                     }
  94.                 })
  95.                 .create();
  96.         alert.show();
  97.     }

  98.     public void show_List2_dialog(){
  99.         final String[] fruits = new String[]{"苹果", "雪梨", "香蕉", "葡萄", "西瓜"};
  100.         alert = null;
  101.         builder = new AlertDialog.Builder(mContext);
  102.         alert = builder.setIcon(R.drawable.nguhyw)
  103.                 .setTitle("选择你喜欢的水果")
  104.                 .setSingleChoiceItems(fruits, 0, new DialogInterface.OnClickListener() {
  105.                     @Override
  106.                     public void onClick(DialogInterface dialog, int which) {
  107.                         Toast.makeText(getApplicationContext(),"你选择了"+fruits[which],Toast.LENGTH_SHORT).show();
  108.                         alert.hide();
  109.                     }
  110.                 }).create();
  111.         alert.show();
  112.     }

  113.     public void show_List3_dialog(){
  114.         final String[] menu = new String[]{"水煮豆腐", "萝卜牛腩", "酱油鸡", "胡椒猪肚鸡"};
  115.         checkItems = new boolean[]{false, false, false, false};

  116.         alert = null;
  117.         builder = new AlertDialog.Builder(mContext);
  118.         alert = builder.setIcon(R.drawable.nguhyw)
  119.                 .setMultiChoiceItems(menu, checkItems, new DialogInterface.OnMultiChoiceClickListener() {
  120.                     @Override
  121.                     public void onClick(DialogInterface dialog, int which, boolean isChecked) {
  122.                         checkItems[which] = isChecked;
  123.                     }
  124.                 })
  125.                 .setPositiveButton("确定", new DialogInterface.OnClickListener() {
  126.                     @Override
  127.                     public void onClick(DialogInterface dialog, int which) {
  128.                         String result = "";
  129.                         for(int i=0; i<checkItems.length;i++){
  130.                             if(checkItems[i])
  131.                                 result += menu[i] + " ";
  132.                         }
  133.                         Toast.makeText(getApplicationContext(), "客官你点了:" + result, Toast.LENGTH_SHORT).show();
  134.                     }
  135.                 }).create();
  136.         alert.show();
  137.     }
  138. }


XML文件

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android=""
  3.     xmlns:tools="" android:layout_width="match_parent"
  4.     android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
  5.     android:paddingRight="@dimen/activity_horizontal_margin"
  6.     android:paddingTop="@dimen/activity_vertical_margin"
  7.     android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

  8.     <LinearLayout
  9.         android:orientation="vertical"
  10.         android:layout_width="match_parent"
  11.         android:layout_height="match_parent"
  12.         android:layout_alignParentTop="true">

  13.         <Button
  14.             android:layout_width="wrap_content"
  15.             android:layout_height="wrap_content"
  16.             android:text="1、显示一个普通的对话框"
  17.             android:id="@+id/btn_normal" />

  18.         <Button
  19.             android:layout_width="wrap_content"
  20.             android:layout_height="wrap_content"
  21.             android:text="2、显示一个列表对话框"
  22.             android:id="@+id/btn_List1" />

  23.         <Button
  24.             android:layout_width="wrap_content"
  25.             android:layout_height="wrap_content"
  26.             android:text="3、显示一个单选的列表"
  27.             android:id="@+id/btn_List2" />

  28.         <Button
  29.             android:layout_width="wrap_content"
  30.             android:layout_height="wrap_content"
  31.             android:text="3、显示一个复选框的列表"
  32.             android:id="@+id/btn_List3" />
  33.     </LinearLayout>
  34. </RelativeLayout>

本次介绍的对话框用法非常简单~无非就是创建一个Builder对象后, 进行相关设置,然后create()生成一个AlertDialog对象,最后调用show()方法将AlertDialog 显示出来而已!另外,细心的你可能发现我们点击对话框的外部区域,对话框就会消失,我们 可以为builder设置setCancelable(false)即可解决这个问题!


阅读(1403) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~