对话框简介

android提供了丰富的对话框支持,支持四种如下的对话框。



AlertDialog简介





介绍上面六个方法的代码示例

setMessage()

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/text02" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="setMessage"
        android:id="@+id/button"
        android:onClick="dialog"/>
</LinearLayout>
package peng.liu.test;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TextView;

public class MainActivity extends Activity {
    TextView text02;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text02 = (TextView) findViewById(R.id.text02);
        Button setMessage = (Button) findViewById(R.id.button);
    }
    public void dialog(View view){
        AlertDialog.Builder builder = new AlertDialog.Builder(this).setIcon(R.drawable.ic_launcher)
                .setTitle("dialog")
                .setMessage("hello world");
        setPositiveButton(builder);
        setNegitiveButton(builder);
        builder.create().show();
    }
    public void setPositiveButton(AlertDialog.Builder builder){
        builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                     text02.setText("单击了确定按钮");
            }
        });
    }
    public void setNegitiveButton(AlertDialog.Builder builder){
        builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                text02.setText("单击了取消按钮");
            }
        });
    }
}

简单列表对话框setItems()

需要传入一个数组或者数组的资源id

package peng.liu.test;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TextView;

public class MainActivity extends Activity {
    TextView text02;
    private String[] items = new String[]{
            "java","python","html","css"
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text02 = (TextView) findViewById(R.id.text02);
        Button setMessage = (Button) findViewById(R.id.button);
    }
    public void dialog(View view){
        AlertDialog.Builder builder = new AlertDialog.Builder(this).setIcon(R.drawable.ic_launcher)
                .setTitle("dialog")
                .setItems(items,new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        text02.setText(items[i]);
                    }
                });
        setPositiveButton(builder);
        setNegitiveButton(builder);
        builder.create().show();
    }
    public void setPositiveButton(AlertDialog.Builder builder){
        builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                     text02.setText("单击了确定按钮");
            }
        });
    }
    public void setNegitiveButton(AlertDialog.Builder builder){
        builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                text02.setText("单击了取消按钮");
            }
        });
    }
}

单选列表对话框setSingleChooseItems

参数是数组,sursor,或者ListAdapter。

package peng.liu.test;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TextView;

public class MainActivity extends Activity {
    TextView text02;
    private String[] items = new String[]{
            "java","python","html","css"
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text02 = (TextView) findViewById(R.id.text02);
        Button setMessage = (Button) findViewById(R.id.button);
    }
    public void dialog(View view){
        AlertDialog.Builder builder = new AlertDialog.Builder(this).setIcon(R.drawable.ic_launcher)
                .setTitle("dialog")
                //1表示第二个框被选中
                .setSingleChoiceItems(items,1,new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        text02.setText(items[i]);
                    }
                });
        setPositiveButton(builder);
        setNegitiveButton(builder);
        builder.create().show();
    }
    public void setPositiveButton(AlertDialog.Builder builder){
        builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                     text02.setText("单击了确定按钮");
            }
        });
    }
    public void setNegitiveButton(AlertDialog.Builder builder){
        builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                text02.setText("单击了取消按钮");
            }
        });
    }
}

多选列表对话框 setMultiChooseItems

参数是数组或者cursor

package peng.liu.test;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TextView;

public class MainActivity extends Activity {
    TextView text02;
    private String[] items = new String[]{
            "java","python","html","css"
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text02 = (TextView) findViewById(R.id.text02);
        Button setMessage = (Button) findViewById(R.id.button);
    }
    public void dialog(View view){
        AlertDialog.Builder builder = new AlertDialog.Builder(this).setIcon(R.drawable.ic_launcher)
                .setTitle("dialog")
                //布尔数组表示第二个和第四个被选中
                .setMultiChoiceItems(items,new boolean[]{false,true,false,true},null);
        setPositiveButton(builder);
        setNegitiveButton(builder);
        builder.create().show();
    }
    public void setPositiveButton(AlertDialog.Builder builder){
        builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                     text02.setText("单击了确定按钮");
            }
        });
    }
    public void setNegitiveButton(AlertDialog.Builder builder){
        builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                text02.setText("单击了取消按钮");
            }
        });
    }
}

自定义列表项对话框 setAdapter

参数是Adapter,该方法和setSingleChooseItems都可以接受Adapter作为参数。

package peng.liu.test;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TextView;

public class MainActivity extends Activity {
    TextView text02;
    private String[] items = new String[]{
            "java","python","html","css"
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text02 = (TextView) findViewById(R.id.text02);
        Button setMessage = (Button) findViewById(R.id.button);
    }
    public void dialog(View view){
        AlertDialog.Builder builder = new AlertDialog.Builder(this).setIcon(R.drawable.ic_launcher)
                .setTitle("dialog")
                //布尔数组表示第二个和第四个被选中
                .setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,items),null);
        setPositiveButton(builder);
        setNegitiveButton(builder);
        builder.create().show();
    }
    public void setPositiveButton(AlertDialog.Builder builder){
        builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                     text02.setText("单击了确定按钮");
            }
        });
    }
    public void setNegitiveButton(AlertDialog.Builder builder){
        builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                text02.setText("单击了取消按钮");
            }
        });
    }
}

自定义View对话框

自定义的任何的View组件

package peng.liu.test;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {
    TextView text02;
    private String[] items = new String[]{
            "java","python","html","css"
    };
    ImageView image;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text02 = (TextView) findViewById(R.id.text02);
        Button setMessage = (Button) findViewById(R.id.button);
        image = new ImageView(this);
        image.setImageResource(R.drawable.ic_launcher);
        image.setScaleType(ImageView.ScaleType.FIT_XY);
        image.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    }
    public void dialog(View view){
        AlertDialog.Builder builder = new AlertDialog.Builder(this).setIcon(R.drawable.ic_launcher)
                .setTitle("dialog")
                //布尔数组表示第二个和第四个被选中
                .setView(image);
    }
    public void setPositiveButton(AlertDialog.Builder builder){
        builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                     text02.setText("单击了确定按钮");
            }
        });
    }
    public void setNegitiveButton(AlertDialog.Builder builder){
        builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                text02.setText("单击了取消按钮");
            }
        });
    }
}

Android对话框AlertDialog-android学习之旅(四十二)的更多相关文章

  1. Android使用局和数据实现天气项目-android学习之旅(十二)

    1.首先注册聚合数据账号,下载相应的sdk 2.导入jar包和 so文件 配置Application,初始化sdk <application //自己新建的application类 androi ...

  2. Android四大组件之一Service介绍-android学习之旅(十二)

    基本概念: service是android四大组件之一,运行在后台执行耗时操作,并不提供用户界面.其他组件如acticity可以通过startService启动该组件,也可以通过bindService ...

  3. Android广播接收器Broadcast Receiver-android学习之旅(十二)

    首先继承BroadcastReceiver类,并在manifest中注册 public class MyReceiver extends BroadcastReceiver { public MyRe ...

  4. Dynamic CRM 2013学习笔记(四十二)流程5 - 实时/同步工作流(Workflow)用法图解

    实时工作流跟插件一样,也是用事件执行管道来执行,能在pre,post或核心操作中执行.跟插件一样,不能在创建之前和删除之后执行.如果执行过程中有异常发生,会取消并回滚整个操作.实时工作流里所有的活动和 ...

  5. Python学习之旅(十二)

    Python基础知识(11):高级特性 一.分片(切片) 通过索引来获取一定范围内的元素 #字符串 s="Alice" s[0:4:2] 结果: 'Ai' #列表 l=[1,2,3 ...

  6. 【WPF学习】第四十二章 透明

    WPF支持真正的透明效果.这意味着,如果在一个性质或元素上层叠另外几个形状或元素,并让所有这些形状和元素具有不同的透明度,就会看到所期望的效果.通过该特性能够创建透过上面的元素可以看到的的图像背景,这 ...

  7. Dynamic CRM 2013学习笔记(四十六)简单审批流的实现

    前面介绍过自定义审批流: Dynamic CRM 2013学习笔记(十九)自定义审批流1 - 效果演示 Dynamic CRM 2013学习笔记(二十一)自定义审批流2 - 配置按钮 Dynamic ...

  8. Gradle 1.12用户指南翻译——第四十二章. Announce插件

    本文由CSDN博客万一博主翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...

  9. NeHe OpenGL教程 第四十二课:多重视口

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  10. JAVA之旅(十二)——Thread,run和start的特点,线程运行状态,获取线程对象和名称,多线程实例演示,使用Runnable接口

    JAVA之旅(十二)--Thread,run和start的特点,线程运行状态,获取线程对象和名称,多线程实例演示,使用Runnable接口 开始挑战一些难度了,线程和I/O方面的操作了,继续坚持 一. ...

随机推荐

  1. Python从入门到实践 学习笔记(二)元祖686gffs

    列表是可以修改的,而不可变的列表被称为元组 . 定义 * 用圆括号来标识.定义元组后,使用索引来访问其元素,就像访问列表元素一样 修改变量 * 不能修改元组的元素,但可以给存储元组的变量赋值 修改元素 ...

  2. 数据结构之Trie树

    1. 概述 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树. Trie一词来自retrieve,发音为/tr ...

  3. CI下载与安装_基础配置_MVC

    CI:CodeIgniter -- 由Ellislab公司的CEORickEllis开发,是一个简单快速的PHP MVC框架. =============下载和安装================地址 ...

  4. 前端性能优化之-dns预解析

    预解析的实现: 1. 用meta信息来告知浏览器, 当前页面要做DNS预解析:<meta http-equiv="x-dns-prefetch-control" conten ...

  5. XML相关知识

    XML的定义:  XML即可扩展标记语言标记是指计算机所能理解的信息符号,通过此种标记,计算机之间可以处理包含各种信息的文章等.如何定义这些标记,既可以选择国际通用的标记语言,比如HTML,也可以使用 ...

  6. win8以上系统查看iis网站进程内存占用情况

    由于win8以上系统在任务管理器中已经屏蔽了具体的IIS网站的进程,在进程以及详细中无法区分是哪个站点了,所以我们需要先知道各站点对应的进程pid,然后再到任务管理器中根据具体的pid查看资源占用情况 ...

  7. Docker 备份、恢复、迁移数据卷

    可以利用数据卷对其中的数据进行进行备份.恢复和迁移. 备份 首先使用 --volumes-from 标记来创建一个加载 dbdata 容器卷的容器,并从本地主机挂载当前到容器的 /backup 目录. ...

  8. Android简易实战教程--第四十九话《满屏拖动的控件》

    今天做个有意思的效果吧,控件的拖拽,简单实用,逻辑清晰点3分钟看完. 说的很高大上,其实就是拖动Button按钮跟着鼠标位置满手机屏幕跑罢了. 直接上简单的代码吧: public class Main ...

  9. Python 2.7 闭包的局限

    想法源自:http://stackoverflow.com/questions/141642/what-limitations-have-closures-in-python-compared-to- ...

  10. Spinner控件详解

    Spinner控件详解 效果图 修改Spinner样式 在介绍之前,先看一下系统原生的样式 6.x & 5.x系统样式 4.x系统样式 官方文档 XML属性 方法 描述 android:dro ...