Android—对话框
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—对话框的更多相关文章
- Android 对话框(Dialog)大全 建立你自己的对话框
Android 对话框(Dialog)大全 建立你自己的对话框 原文地址: http://www.cnblogs.com/salam/archive/2010/11/15/1877512.html A ...
- Android对话框
这周过的实在是艰辛,自打这周二起我的本本就开始闹"罢工",最后还是重装系统了事. . . 只是可怜了我的那些被格了的软件(悲伤辣么大)! 往事不要再提,人生几度风雨... 简 ...
- Android对话框和帧动画
Android对话框 在一个例子中展示四种对话框. 设置四个按钮 <LinearLayout xmlns:android="http://schemas.android.com/apk ...
- Android对话框(Dialog)
Android对话框 前几天出差没有进行更新,今天写一下安卓中用的比较多的对话框——AlertDialog. dialog就是一个在屏幕上弹出一个可以让用户做出一个选择,或者输入额外的信息的对话框,一 ...
- Android对话框自定义标题
Android自带的对话框标题不好看,如果我们需要给弹出的对话框设置一个自己定义的标题,可以使用AlertDialog.Builder的setCustomTitle()方法. 定义一个对话框标题的ti ...
- Android对话框之dismiss和cancel和hide区别
在我们看来两者效果都是一样的,其实看下源码就知道cancel肯定会去调dismiss的,如果调用的cancel的话就可以监听DialogInterface.OnCancelListener. /** ...
- 转 Android 对话框(Dialog)大全 建立你自己的对话框
Activities提供了一种方便管理的创建.保存.回复的对话框机制,例如 onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog( ...
- Android 对话框弹出位置和透明度的设置
在Android中 我们经常会用AlertDialog来显示对话框.通过这个对话框是显示在屏幕中心的.但在某些程序中,要求对话框可以显示在不同的位置.例如,屏幕的上 方或下方.要实现这种效果.就需要获 ...
- Android 对话框用法
来自:http://www.cnblogs.com/salam/archive/2010/11/15/1877512.html Activities提供了一种方便管理的创建.保存.回复的对话框机制,例 ...
随机推荐
- SharePoint开发 - 自定义导航菜单(二)母版页的菜单应用
博客地址 http://blog.csdn.net/foxdave 接上篇点击打开链接 本篇叙述在母版页中应用之前的配置信息生成菜单,主要涉及到母版页的自定义,并应用了第三方控件库DevExpress ...
- objective-c strong导致内存泄漏简单案例
例如: @interface Test:NSObject{ id __strong obj_; } -(void) setObject:(id __strong)obj; @end @implemen ...
- 《day14---多线程入门_进阶》
/* 多线程: 进程:正在执行中的程序,一个应用程序启动后在内存中运行的那片空间.进程具有动态性和并发性. 线程:进程中的一个执行单元.负责进程中的程序的运行的.一个进程中至少要有一个线程. 一个进程 ...
- C# 使用命令行编译单个CS文件
编译单个CS文件. 1.编译 File.cs 以产生 File.exe: csc File.cs 2.编译 File.cs 以产生 File.dll: ...
- hdu 2096
PS:做不出前面几道题...很不爽..扒拉了几下找了简单题来做.... #include "stdio.h" int cal(int a); int main(){ int a,b ...
- 2016 - 1 -19 初探NSOperation
一:简介 1.NSOperation的作用: 配合NSOperation与NSOperationQueue也可以实现多线程. 2.NSOperation与NSOperationQueue实现多线程的步 ...
- Mac可设置环境变量的位置、查看和添加PATH环境变量
Mac 启动加载文件位置(可设置环境变量) 首先要知道你使用的 Mac OS X 是什么样的 Shell,使用命令 echo $SHELL 如果输出的是:csh 或者是 tcsh,那么你用的就是 C ...
- 20145210 《Java程序设计》第07周学习总结
教材学习内容总结 第十二章 Lambda 12.1 认识Lambda语法 •Lambda 教材的引入循序渐近.深入浅出 •如果使用JDK8的话,可以使用Lambda特性去除重复的信息,例: Compa ...
- java作业4
(一) 请查看String.equals()方法的实现代码,注意学习其实现方法.(发表到博客作业上) (二) 整理String类的Length().charAt(). getChars().rep ...
- [SYSU]每周一赛
2014年每周一赛第一场 A.Cutting Sausages B.Rectangular Fields //待做 ...