layout文件:

 <?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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.hanqi.testapp2.TestActivity5"
android:orientation="vertical"> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="一般对话框"
android:onClick="bt1_onClick"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="bt2_onClick"
android:text="单选对话框"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="bt3_onClick"
android:text="复选对话框"/>
</LinearLayout>

java类代码:

 package com.hanqi.testapp2;

 import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast; public class TestActivity5 extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test5);
}
//一般对话框
public void bt1_onClick(View v)
{
//对话框不能直接实例化
//内部提供了构造器
//方法链调用
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setTitle("确认对话框")
.setMessage("确实要删除么")
.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(TestActivity5.this, "执行删除,which = " + which, Toast.LENGTH_SHORT).show();
}
})//正面按钮
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(TestActivity5.this, "取消删除,which = " + which, Toast.LENGTH_SHORT).show();
}
})
.setNeutralButton("中立", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(TestActivity5.this, "按钮,which = " + which, Toast.LENGTH_SHORT).show();
}
}).setCancelable(false)
.show();//显示对话框
}
//单选对话框
public void bt2_onClick(View v)
{
//final可以让常量的生命周期延长到整个实例
final String [] str = {"男","女"};
new AlertDialog.Builder(this)
.setTitle("单选对话框")
.setSingleChoiceItems(str, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(TestActivity5.this, "which = "+which+
",选中的是"+str[which], Toast.LENGTH_SHORT).show();
//关闭对话框
dialog.dismiss();
}
}).setCancelable(false)
.show();
}
//复选对话框
public void bt3_onClick(View v)
{
final String [] str = {"宝马","奔驰","劳斯莱斯","宾利"};
final boolean[] ch = {true,false,false,true};
new AlertDialog.Builder(this)
.setTitle("复选对话框")
.setMultiChoiceItems(str, ch, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
//改变对应数组项的选中状态
ch[which] = isChecked;
}
})
.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int i = 0;
//获取最终的选中状态
for (boolean b:ch)
{
Toast.makeText(TestActivity5.this, str[i]+"选中状态 = "+
(b?"选中":"未选中"), Toast.LENGTH_SHORT).show();
i++;
}
}
})
.setNegativeButton("取消", null)
.setCancelable(false)
.show();
}
}

效果图:

Android—对话框的更多相关文章

  1. Android 对话框(Dialog)大全 建立你自己的对话框

    Android 对话框(Dialog)大全 建立你自己的对话框 原文地址: http://www.cnblogs.com/salam/archive/2010/11/15/1877512.html A ...

  2. Android对话框

    这周过的实在是艰辛,自打这周二起我的本本就开始闹"罢工",最后还是重装系统了事. . .   只是可怜了我的那些被格了的软件(悲伤辣么大)!  往事不要再提,人生几度风雨... 简 ...

  3. Android对话框和帧动画

    Android对话框 在一个例子中展示四种对话框. 设置四个按钮 <LinearLayout xmlns:android="http://schemas.android.com/apk ...

  4. Android对话框(Dialog)

    Android对话框 前几天出差没有进行更新,今天写一下安卓中用的比较多的对话框——AlertDialog. dialog就是一个在屏幕上弹出一个可以让用户做出一个选择,或者输入额外的信息的对话框,一 ...

  5. Android对话框自定义标题

    Android自带的对话框标题不好看,如果我们需要给弹出的对话框设置一个自己定义的标题,可以使用AlertDialog.Builder的setCustomTitle()方法. 定义一个对话框标题的ti ...

  6. Android对话框之dismiss和cancel和hide区别

    在我们看来两者效果都是一样的,其实看下源码就知道cancel肯定会去调dismiss的,如果调用的cancel的话就可以监听DialogInterface.OnCancelListener. /** ...

  7. 转 Android 对话框(Dialog)大全 建立你自己的对话框

    Activities提供了一种方便管理的创建.保存.回复的对话框机制,例如 onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog( ...

  8. Android 对话框弹出位置和透明度的设置

    在Android中 我们经常会用AlertDialog来显示对话框.通过这个对话框是显示在屏幕中心的.但在某些程序中,要求对话框可以显示在不同的位置.例如,屏幕的上 方或下方.要实现这种效果.就需要获 ...

  9. Android 对话框用法

    来自:http://www.cnblogs.com/salam/archive/2010/11/15/1877512.html Activities提供了一种方便管理的创建.保存.回复的对话框机制,例 ...

随机推荐

  1. auto_ptr的使用原则

    auto_ptr是c++标准库中的一种严格所有权型的智能指针,实现在backward/auto_ptr.h文件中 pro: 1.做临时变量时,不需要手动去释放资源 void f() { ClassA ...

  2. OpenCV之响应鼠标(三):响应鼠标信息

    转自:http://blog.csdn.net/haihong84/article/details/6599838 程序代碼如下: #include <cv.h>#include < ...

  3. SpringMvc中的反射

    controller中的方法,是通过反射调用的 spring监控controller中的注解,当命令符合某个注解的时候,通过反射,找到这个注解对应的方法,然后调用,处理完成得到返回值,再根据这个返回值 ...

  4. poj2193

    //Accepted 368K 532MS //线性dp //dp[i][j]表示前i位最后一个是j的排列数 //dp[i][j]=sum(dp[i-1][h]) h*2<=j #include ...

  5. 团队博客——Sprint计划会议1

    每日Scrum:第一天 会议时间:4.14.晚八点半 会议地点:基础教学楼一楼大厅 小组成员:郭庆樑,林彦汝,张金 认领人—使团队成员分工合作,保持团队的积极性. ID 名称(NAME) 重要性(IM ...

  6. objectARX判断当前坐标系

    判断当前坐标系是WCS还是UCS 使用系统变量 WORLDUCS   请参见 用户坐标系 (UCS) 概述 (只读) 类型: 整数 保存位置: 未保存 初始值: 1 指示 UCS 是否与 WCS 相同 ...

  7. (转)MyEclipse设置注释格式

    原文:http://xinghaifeng2006.iteye.com/blog/1243565 MyEclipse设置注释格式(转载)          博客分类: Java基础知识   Windo ...

  8. a various of context

    ContextWrapper.getApplicationContext():Return the context of the single, global Application object o ...

  9. 团队开发——冲刺1.a

    冲刺阶段一(第一天) 1.今天准备做什么? 在了解C#的基础上,深入熟悉Windows窗体应用程序,熟练掌握基本功能. 2.明天做什么:简单设计界面.

  10. BZOJ 2054 疯狂的馒头

    并查集把染过色的并在一起.倒着染色. #include<iostream> #include<cstdio> #include<cstring> #include& ...