Android中Dialog对话框
布局文件xml:
<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"
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=".DialogActivity" > <Button
android:id="@+id/plainDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="普通Dialog" /> <Button
android:id="@+id/plainDialogEvent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dialog按钮事件集中处理" /> <Button
android:id="@+id/inputDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请输入框" /> <Button
android:id="@+id/listDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="列表对话框" /> <Button
android:id="@+id/radioDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="单选对话框" /> <Button
android:id="@+id/checkboxDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="多选对话框" /> <Button
android:id="@+id/diyDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义布局对话框" /> </LinearLayout>
Activity文件:
普通的dialog:
private void plainDialogDemo() { Button plainBtn = (Button) findViewById(R.id.plainDialog);
plainBtn.setOnClickListener(new OnClickListener() { public void onClick(View v) { new AlertDialog.Builder(DialogActivity.this)
.setTitle("删除")
.setMessage("确定删除指定数据")
.setPositiveButton("确定",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog,
int which) {
Toast.makeText(getApplicationContext(),
"确定了", Toast.LENGTH_SHORT)
.show();
}
})
.setNegativeButton("取消",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog,
int which) {
}
}).setCancelable(false).show();
}
});
}
效果如下:
输入文本框的dialog:
private void inputDialog() {
Button inputBtn = (Button) findViewById(R.id.inputDialog);
inputBtn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
final EditText et = new EditText(DialogActivity.this);
new AlertDialog.Builder(DialogActivity.this)
.setTitle("请输入数字")
.setView(et)
.setPositiveButton("确定",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
et.getText(),
Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("取消", null)
.setCancelable(false).show();
}
});
}
效果如下:
列表dialog:
private void listDialogDemo() {
Button listBtn = (Button) findViewById(R.id.listDialog);
listBtn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
final String[] names = { "C罗", "J罗", "H罗" };
new AlertDialog.Builder(DialogActivity.this).setTitle("列表对话框")
.setItems(names, new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog,
int which) {
Toast.makeText(DialogActivity.this,
names[which], Toast.LENGTH_SHORT)
.show();
}
}).setNegativeButton("取消", null).show();
}
});
}
效果如下:
单选dialog:
private void radioDialogDemo() {
Button radioButton = (Button) findViewById(R.id.radioDialog);
radioButton.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) { final String[] names = { "C罗", "J罗", "H罗" };
new AlertDialog.Builder(DialogActivity.this)
.setTitle("列表对话框")
.setSingleChoiceItems(names, 0,
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog,
int which) { selecteName = names[which];
}
})
.setPositiveButton("确定",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog,
int which) { Toast.makeText(DialogActivity.this,
selecteName, Toast.LENGTH_SHORT)
.show();
}
}).setNegativeButton("取消", null).show();
}
});
}
效果如下:
多选dialog:
private void checkDialogDemo() {
Button checkBtn = (Button) findViewById(R.id.checkboxDialog);
checkBtn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
final String[] names = { "C罗", "J罗", "H罗" };
final boolean[] selected = new boolean[] { true, false, true };
new AlertDialog.Builder(DialogActivity.this)
.setMultiChoiceItems(
names,
selected,
new DialogInterface.OnMultiChoiceClickListener() { @Override
public void onClick(DialogInterface dialog,
int which, boolean isChecked) { }
})
.setPositiveButton("确定",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog,
int which) {
StringBuilder sb = new StringBuilder(
"你选择了:");
for (int i = 0; i < names.length; i++) {
if (selected[i]) {
sb.append(names[i]);
}
}
Toast.makeText(DialogActivity.this,
sb.toString(), 1).show();
}
}).setNegativeButton("取消", null).show();
}
});
}
效果如下:
自定义dialog:
private void customDialogDemo() {
final AlertDialog dlg = new AlertDialog.Builder(this).create();
dlg.show();
Window window = dlg.getWindow();
window.setContentView(R.layout.diylayout);
ImageButton ok = (ImageButton) window.findViewById(R.id.btnok);
ok.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "关闭了",
Toast.LENGTH_SHORT).show();
dlg.dismiss();
}
});
}
自定义布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <ImageView
android:id="@+id/dialogimg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/dialog_bg" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/dialogimg"
android:layout_alignTop="@id/dialogimg"
android:layout_marginLeft="50dp"
android:layout_marginTop="60dp"
android:text="自定义的dialog" /> <ImageButton
android:id="@+id/btnok"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignRight="@id/dialogimg"
android:layout_alignTop="@id/dialogimg"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:background="@drawable/close_dialog" /> </RelativeLayout>
效果如下:
Android中Dialog对话框的更多相关文章
- Android中Dialog对话框的调用及监听
Android中经常会需要在Android界面上弹出一些对话框提示用户,比如App的退出的时候都会有各种框来挽留你的心,支付宝的时候输入密码的密码框,非常常见及其实用的功能,类似于JS中的alter, ...
- android从Dialog对话框中取得文本文字
android中Dialog对话框获取文本文字,只需要使用editor的getText方法就可以获得,示例如下:final EditText et = new EditText(this); et.s ...
- Android中Dialog
在Android中,Dialog是一个非常重要的UI, 它可以方便的给用户提示,用最简洁的方式向用户展示信息, 以下的图片是Dialog的一个整体架构,通过它,可以总体对Dialog有一个很清晰的认识 ...
- Android中的对话框AlertDialog使用技巧合集-转载
Android中的对话框AlertDialog使用技巧合集 文章来自:http://blog.csdn.net/blue6626/article/details/6641105 今天我用自 ...
- Android 中Dialog的使用
本文是参考ProAndroid的第10章Working with Dialogs的内容,在合适的地方添加了作者自己的一些见解最终成文. Android 中的对话框是一个展示在当前窗口上的小一号的窗口, ...
- [原]MFC中DIALOG(对话框)程序响应加速键(快捷键)
[原]MFC中DIALOG(对话框)程序响应加速键(快捷键) 2014-8-6阅读266 评论0 新建一个对话框程序,项目名为Test,删除默认确定,取消和静态文本框控件.添加一个按钮,Caption ...
- android中常见对话框之一AlertDialog
在Android应用中,有多种对话框:Dialog.AlertDialog.ProgressDialog.时间.日期等对话框. (1)Dialog类,是一切对话框的基类,需要注意的是,Dialog类虽 ...
- Android中Dialog的使用
上一篇博文讲到对话框popWindow的使用,这篇博文主要解说Dialog的使用. 1.什么是Dialog? Dialog就是对话框的一种方式! 在Android开发中.我们常常会须要在Android ...
- Android自定义 Dialog 对话框
Android自定义Dialoghttp://www.cnblogs.com/and_he/archive/2011/09/16/2178716.html Android使用自定义AlertDialo ...
随机推荐
- jmx使用应该注意的基本规范
1.标准MBean 名称必需是在要监控的类名后面加上“MBean ”. 2.监控的类和MBean 接口必需在同一包下,也可以理解为注册的接口名字必须以MBean结尾,接口实现类比接口名字少了MBean ...
- css3动画之animate
CSS3 动画属性 下面的表格列出了 @keyframes 规则和所有动画属性: 语法:div{animation: 动画名称 一个周期播放时间 速度曲线 延迟时间 下个周期是否逆向} @keyfra ...
- CentOS用gparted实现无损调整磁盘分区大小
作者: sheldon 测试服务器硬盘挂载在/usr下的分区大小只有10G,随着必须软件都安装在这个目录下,这个分区已经满额,给分区扩容刻不容缓,window下有PQ分区工具,Linux下也有gpar ...
- MVC 5 + EF 6
(一) ??运算符 C#中两个问号(“?”)的作用是判断“?”左边的对象是否为null,如果不为null则使用“?”左边的对象,如果为null则使用“?”右边的对象. (二)VS安装Entity Fr ...
- c#操作sqlite
一.添加选中dll引用如下图 二.下载一个sqlite建表建库工具sqlitedatabasebrowser如下图 三.使用sqlitedatabasebrowser建库建表 四.插入表数据如下图 四 ...
- myql 注意事项
在[mysqld]下加入一行:lower_case_table_names=1,1为不区分大小写,0是区分大小写...并/etc/init.d/mysql restart即可...
- C#6 冒泡 折半查找 二维数组
人类思维--计算机逻辑思维 逻辑思维--代码实现 写书法: 描红--临摹--碑贴--自成一体--草 章节复习: 数组:一维,二维,多维 一维:豆角.连续,同一类型. 定义:数据类型[] 数组名=new ...
- Cordova了解
概述 Cordova让我们可以使用HTML.JS以及CSS开发跨平台App的公共部分(整个App就是一个WebView或者或者嵌入到本地组件中),与原生API的交互通过Cordova插件实现. 安装配 ...
- Javascript Number类型常见迷惑点
1:NaN(Not a Number) 表示一个本来要返回数值的操作数没有返回数值的情况.在ECMAscript中,任何数除以0会返回NaN[ps:实际上只有0/0会返回NaN],正(负)数除以0会返 ...
- 如何获取浏览器URL中查询字符串的参数
首先要知道Location这个对象以及这个对象中的一些属性: href:设置或返回完整的url.如本博客首页返回http://www.cnblogs.com/wymninja/ host:设置或返回主 ...