一.单选对话框

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.example.wang.testapp2.TestActivity5"
11     android:orientation="vertical">
12 
13 
14     <Button
15         android:layout_width="match_parent"
16         android:layout_height="wrap_content"
17         android:text="单选对话框"
18         android:onClick="bt2_OnClick"
19         />
20 
21 </LinearLayout>
一般对话框
 1 package com.example.wang.testapp2;
 2 
 3 import android.app.AlertDialog;
 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.Toast;
 9 
10 public class TestActivity5 extends AppCompatActivity {
11 
12     @Override
13     protected void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.activity_test5);
16 
17     }
18 
19 
20     //单选对话框
21     public  void bt2_OnClick(View v)
22     {
23         //final  可以使这个常量的生命周期延长到整个对象
24         final String[] str={"男","女"};
25 
26         new  AlertDialog.Builder(this)
27                 .setTitle("单选对话框")
28                 .setSingleChoiceItems(str, 0, new DialogInterface.OnClickListener() {
29                     @Override
30                     public void onClick(DialogInterface dialog, int which) {
31 
32                         Toast.makeText(TestActivity5.this, "which="+which+",选中的是"+str[which], Toast.LENGTH_SHORT).show();
33 
34                         //点击关闭对话框
35                         //a.dismiss();
36                         dialog.dismiss();
37                     }
38                 })
39                 .setCancelable(false)
40                 .show();
41     }
42 
43 }
单选对话框

二.复选对话框

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.example.wang.testapp2.TestActivity5"
11     android:orientation="vertical">
12 
13 
14     <Button
15         android:layout_width="match_parent"
16         android:layout_height="wrap_content"
17         android:text="复选对话框"
18         android:onClick="bt3_OnClick"
19         />
20 
21 </LinearLayout>
复选对话框
 1 package com.example.wang.testapp2;
 2 
 3 import android.app.AlertDialog;
 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.Toast;
 9 
10 public class TestActivity5 extends AppCompatActivity {
11 
12     @Override
13     protected void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.activity_test5);
16 
17     }
18 
19     //复选对话框
20     public  void bt3_OnClick(View v)
21     {
22         final String[] str={"宝马","奔驰","劳斯莱斯","宾利"};
23         final boolean[] ch={true,false,false,true};
24 
25         new AlertDialog.Builder(this)
26                 .setTitle("复选对话框")
27                 .setMultiChoiceItems(str, ch, new DialogInterface.OnMultiChoiceClickListener() {
28                     @Override
29                     public void onClick(DialogInterface dialog, int which, boolean isChecked) {
30 
31                         //改变对应数组项的选中状态
32                         ch[which]=isChecked;
33                     }
34                 })
35                 .setPositiveButton("确认", new DialogInterface.OnClickListener() {
36                     @Override
37                     public void onClick(DialogInterface dialog, int which) {
38 
39                         int i=0;
40                         //获取最终的选中状态
41                         for (boolean b:ch)
42                         {
43                             Toast.makeText(TestActivity5.this, str[i]+"选中状态="+(b?"选中":"未选中"), Toast.LENGTH_SHORT).show();
44 
45                             i++;
46                         }
47                     }
48                 })
49                 .setNegativeButton("取消", null)
50                 .setCancelable(false)
51                 .show();
52     }
53 
54 }
复选对话框

 

posted on 2016-05-12 18:56  安然罒  阅读(262)  评论(0编辑  收藏  举报