码迷,mamicode.com
首页 > 移动开发 > 详细

Android—对话框

时间:2016-05-13 11:02:23      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

layout文件:

 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.hanqi.testapp2.TestActivity5"
11     android:orientation="vertical">
12 
13     <Button
14         android:layout_width="match_parent"
15         android:layout_height="wrap_content"
16         android:text="一般对话框"
17         android:onClick="bt1_onClick"/>
18     <Button
19         android:layout_width="match_parent"
20         android:layout_height="wrap_content"
21         android:onClick="bt2_onClick"
22         android:text="单选对话框"/>
23     <Button
24         android:layout_width="match_parent"
25         android:layout_height="wrap_content"
26         android:onClick="bt3_onClick"
27         android:text="复选对话框"/>
28 </LinearLayout>

java类代码:

 1 package com.hanqi.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     public void bt1_onClick(View v)
19     {
20         //对话框不能直接实例化
21         //内部提供了构造器
22         //方法链调用
23         AlertDialog alertDialog = new AlertDialog.Builder(this)
24                 .setTitle("确认对话框")
25                 .setMessage("确实要删除么")
26                 .setPositiveButton("确认", new DialogInterface.OnClickListener() {
27                     @Override
28                     public void onClick(DialogInterface dialog, int which) {
29                         Toast.makeText(TestActivity5.this, "执行删除,which = " + which, Toast.LENGTH_SHORT).show();
30                     }
31                 })//正面按钮
32                 .setNegativeButton("取消", new DialogInterface.OnClickListener() {
33                     @Override
34                     public void onClick(DialogInterface dialog, int which) {
35                         Toast.makeText(TestActivity5.this, "取消删除,which = " + which, Toast.LENGTH_SHORT).show();
36                     }
37                 })
38                 .setNeutralButton("中立", new DialogInterface.OnClickListener() {
39                     @Override
40                     public void onClick(DialogInterface dialog, int which) {
41                         Toast.makeText(TestActivity5.this, "按钮,which = " + which, Toast.LENGTH_SHORT).show();
42                     }
43                 }).setCancelable(false)
44                 .show();//显示对话框
45     }
46     //单选对话框
47     public void bt2_onClick(View v)
48     {
49         //final可以让常量的生命周期延长到整个实例
50         final String [] str = {"男","女"};
51         new AlertDialog.Builder(this)
52                 .setTitle("单选对话框")
53                 .setSingleChoiceItems(str, 0, new DialogInterface.OnClickListener() {
54                     @Override
55                     public void onClick(DialogInterface dialog, int which) {
56                         Toast.makeText(TestActivity5.this, "which = "+which+
57                                 ",选中的是"+str[which], Toast.LENGTH_SHORT).show();
58                         //关闭对话框
59                         dialog.dismiss();
60                     }
61                 }).setCancelable(false)
62                 .show();
63     }
64     //复选对话框
65     public void bt3_onClick(View v)
66     {
67         final String [] str = {"宝马","奔驰","劳斯莱斯","宾利"};
68         final boolean[] ch = {true,false,false,true};
69         new AlertDialog.Builder(this)
70                 .setTitle("复选对话框")
71                 .setMultiChoiceItems(str, ch, new DialogInterface.OnMultiChoiceClickListener() {
72                     @Override
73                     public void onClick(DialogInterface dialog, int which, boolean isChecked) {
74                         //改变对应数组项的选中状态
75                         ch[which] = isChecked;
76                     }
77                 })
78                 .setPositiveButton("确认", new DialogInterface.OnClickListener() {
79                     @Override
80                     public void onClick(DialogInterface dialog, int which) {
81                         int i = 0;
82                         //获取最终的选中状态
83                         for (boolean b:ch)
84                         {
85                             Toast.makeText(TestActivity5.this, str[i]+"选中状态 = "+
86                                     (b?"选中":"未选中"), Toast.LENGTH_SHORT).show();
87                             i++;
88                         }
89                     }
90                 })
91                 .setNegativeButton("取消", null)
92                 .setCancelable(false)
93                 .show();
94     }
95 }

效果图:

技术分享技术分享技术分享技术分享

Android—对话框

标签:

原文地址:http://www.cnblogs.com/hanazawalove/p/5486168.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!