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提供了一种方便管理的创建.保存.回复的对话框机制,例 ...
随机推荐
- jQuery Ajax之load()方法
jQuery对Ajax操作进行了封装,在jQuery中$.ajax()方法属于最底层的方法,第2层是load().$.get()和$.post()方法,第3层是$.getScript()和$.getJ ...
- servlet filter可以用注解
现在好像可以在新建一个servlet.filter等的的时候在选项中设置urlmapping,通过注解的方式来监控action,以及设置初始参数initparameter.
- MongoDB error: couldn't connect to server 127.0.0.1:27017 src/mongo/shell/mongo.js(转)
rror: couldn't connect to server 127.0.0.1:27017 src/mongo/shell/mongo.js 一般这种情况就是:自己指定的数据库,所以不能.自动加 ...
- (转)MyEclipse设置注释格式
原文:http://xinghaifeng2006.iteye.com/blog/1243565 MyEclipse设置注释格式(转载) 博客分类: Java基础知识 Windo ...
- hdu 2022
Ps:麻蛋...第一次想得太复杂了..用字符串组来存.越弄越傻逼...后来用int就行了... 代码: #include "stdio.h"#include "stdli ...
- flex css 布局
http://www.w3cplus.com/css3/flexbox-basics.html
- 爬虫再探实战(四)———爬取动态加载页面——请求json
还是上次的那个网站,就是它.现在尝试用另一种办法——直接请求json文件,来获取要抓取的信息. 第一步,检查元素,看图如下: 过滤出JS文件,并找出包含要抓取信息的js文件,之后就是构造request ...
- ERP登录(八)
登录的存储过程: ALTER PROCEDURE [dbo].[UserLogin] @userid int output, @LoginName nvarchar(50), @Password nv ...
- swift系统学习第三章
第九节:结构体-sturt //: Playground - noun: a place where people can play import UIKit /* swift学习第九节 结构体:st ...
- PhP访问mysql数据库的基本方式
一,查询 <?php$conn= mysql_connect("127.0.0.1","lanou12","lanou12");$fi ...