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

Android 自学之对话框

时间:2014-08-22 16:10:29      阅读:346      评论:0      收藏:0      [点我收藏+]

标签:des   android   cWeb   style   blog   http   color   java   使用   

Android为我们提供了丰富的对话框支持,提供了四种常用的对话框:

  1. AlertDialog:功能丰富、实际应用最广泛的对话框。

  2. ProgressDialog:进度对话框,该对话框只用于简单的进度条封装。

  3. DatePickerDialog:日期选择对话框,该对话框只对DatePicker包装。

  4. TimePickerDialog:时间选择对话框,该对话框只对TimePicker包装。

上面四种对话框中功能最强用法最灵活的就是AlertDialog,这里详细的介绍下AlertDialog。

一、使用AlertDialog创建简单的对话框

AlertDialog的功能强大,它提供了一些方法来生成四种预定义对话框,这四种对话框分别是:

  1. 带消息、带N个按钮的提示对话框
  2. 带列表、带N个按钮的列表对话框
  3. 带多个单选列表项,带N个按钮的对话框
  4. 带多个多选列表项,带N个按钮的对话框

除此以外,AlertDialog也可以创建界面自定义的对话框。

 

使用AlertDialog创建对话框的步骤大致如下:

  1. 创建AlertDialog.Builder对象,该对象是AlertDialog的创建器
  2. 调用AlertDialog.Builder的方法为对话框设置图标、标题、内容
  3. 调用AlertDialog.Builder的create()方法创建AlertDialog对话框
  4. 调用AlertDialog的show()方法显示对话框

下面通过一个显示提示消息对话框的案例,来看看AlertDialog的用法:程序的界面上有个一个文本框和一个按钮,当用户点击按钮的时候将会显示普通对话框。

先看看布局文件:Layout/main.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6     
 7     <EditText 
 8         android:id="@+id/show"
 9         android:layout_width="fill_parent"
10         android:layout_height="wrap_content"
11         android:editable="false"/>
12 <!-- android:editable设置是否可编辑 -->
13 
14     <Button 
15         android:id="@+id/bn01"
16         android:layout_width="wrap_content"
17         android:layout_height="wrap_content"
18         android:text="显示对话框"/>
19 </LinearLayout>

主程序文件:DialogTest.java

 1 package com.yangjing.dialogtest;
 2 
 3 import android.app.Activity;
 4 import android.app.AlertDialog;
 5 import android.app.AlertDialog.Builder;
 6 import android.content.DialogInterface;
 7 import android.content.DialogInterface.OnClickListener;
 8 import android.os.Bundle;
 9 import android.view.View;
10 import android.widget.Button;
11 import android.widget.EditText;
12 
13 
14 
15 public class DialogTest extends Activity{
16     
17     @Override
18     protected void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.main);
21         Button bn = (Button) findViewById(R.id.bn01);
22         //定义一个AlertDialog.Builder对象
23         final Builder builder = new AlertDialog.Builder(this);
24         //为按钮绑定事件监听器
25         bn.setOnClickListener(new View.OnClickListener() {
26             
27                 @Override
28                 public void onClick(View source) {
29                     // 设置对话框的图标
30                     builder.setIcon(R.drawable.ic_launcher);
31                     // 设置对话框的标题
32                     builder.setTitle("自定义普通的消息提示对话框");
33                     // 设置对话框显示的内容
34                     builder.setMessage("这是一个由AlertDialog定义出来的普通对话框");
35                     // 为对话框设置一个“确定”按钮
36                     builder.setPositiveButton(
37                             "确定",
38                             //为列表项的单击事件设置监听器
39                             new OnClickListener() {
40                             @Override
41                             public void onClick(DialogInterface arg0, int arg1) {
42                                 EditText show = (EditText) findViewById(R.id.show);
43                                 show.setText("您刚刚点击了确定按钮!");
44                             }
45 
46                     });
47                     // 为对话框设置一个“取消”按钮
48                     builder.setNegativeButton(
49                             "取消",new OnClickListener() {
50                                 
51                         @Override
52                         public void onClick(DialogInterface arg0, int arg1) {
53                             EditText show = (EditText) findViewById(R.id.show);
54                             show.setText("您刚刚点击了取消按钮!");
55                         }
56                     });
57                     
58                     builder.create().show();
59                 }
60             
61         });
62     }
63 }

运行的效果:                              

bubuko.com,布布扣bubuko.com,布布扣

当用户点击了确定后,界面上EditText上会显示:您刚刚点击了确定按钮!,若是点了取消按钮的话,则会显示:您刚刚点击了取消按钮!

二、使用AlertDialog创建列表的对话框

AlertDialog.Builder除了提供setMessage方法来设置对话框所显示的消息以外,还提供了如下方法来设置对话框显示列表内容:

  • setItems(int itemsId,DialogInterface.OnClickListener listener): 创建普通列表对话框
  • setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, DialogInterface.OnMultiChoiceListener listener): 创建多选列表对话框
  • setSingleChoiceItems(CharSequence[] items, int checkedItem, DialogInterface.OnClickListener listener): 创建单选列表对话框
  • setAdapter(ListAdapter adapter,DialogInterface.OnClickListener listener): 创建根据ListAdapter提供的列表项的列表对话框

1、下面通过一个普通的列表对话框的案例,来看看setItems方法的用法:

程序的界面上有个一个文本框和一个按钮,当用户点击按钮的时候将会改变文本框的背景颜色

先看看我们简单的布局文件:Layout/main.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:orientation="vertical" >
 6     
 7 <TextView
 8     android:id="@+id/show" 
 9     android:layout_width="fill_parent" 
10     android:layout_height="wrap_content" 
11     android:text="根据你选择的颜色而发生改变"
12     android:textSize="11pt"
13     />
14 <Button 
15     android:id="@+id/bn" 
16     android:layout_width="wrap_content" 
17     android:layout_height="wrap_content" 
18     android:text="选择颜色"
19     />    
20 </LinearLayout>

主程序:ListDialogTest.java

package com.yangjing.listdialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ListDialogTest extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button bn = (Button) findViewById(R.id.bn);
        final Builder builder = new AlertDialog.Builder(this);
        bn.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                //设置对话框的图标
                builder.setIcon(R.drawable.ic_launcher);
                //设置对话框的标题
                builder.setTitle("简单列表对话框");
                //为列表框设置多个列表
                //setItems(int itemsId,DialogInterface.OnClickListener listener): 创建普通列表对话框
                builder.setItems(
                    new String[]{"红色","绿色","蓝色"},
                    new OnClickListener() {
                        //该方法的which参数代表用户单击了那个列表项
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                                TextView show = (TextView) findViewById(R.id.show);
                                switch (which) {
                                case 0:
                                    show.setBackgroundColor(Color.RED);
                                    break;
 
                                case 1:
                                    show.setBackgroundColor(Color.GREEN);
                                    break;
                                    
                                case 2:
                                    show.setBackgroundColor(Color.BLUE);
                                    break;
                                }
                        }
                    }
                );
                builder.create().show();
            }
        });
    }
}

效果展示:

bubuko.com,布布扣bubuko.com,布布扣

 

 

Android 自学之对话框

标签:des   android   cWeb   style   blog   http   color   java   使用   

原文地址:http://www.cnblogs.com/Yang-jing/p/3929535.html

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