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 ...
随机推荐
- properties文件value换行处理方式
书写方式如下,就可以允许key1的value值换行了,但是整个过程要注意不要在文件中出现任何的非英文非半角的字符 key1=Where did you take the picture?\ ...
- 引言:Canvas绘图API快速入门
引言:Canvas绘图API快速入门 在接触HTML5的初学者包括我都在很多地方见到非常炫的一些页面,甚至好多学习HTML5的开发者都是冲着Web端的页游去的,那么HTML5那么绚丽的页面效果以及游戏 ...
- Google Map 根据坐标 获取地址信息
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.X ...
- asp.net 2.0 Session丢失问题
可行的解决方法(本人已用): 1.Web.config文件修改sessionstate模式(默认为InProc) <sessionState mode="/> 2.开启ASP.N ...
- 01js高级_1
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- iOS设计模式解析(六)代理模式
代理模式:为其他对象创建一个代理以控制对这个对象的访问 UML解释:客户端向Proxy发起一个Request()请求,Proxy对象会把这个Request转发给Proxy对象的RealSubject. ...
- JavaScript function函数种类(转)
转自:http://www.cnblogs.com/polk6/p/3284839.html JavaScript function函数种类 本篇主要介绍普通函数.匿名函数.闭包函数 目录 1. 普通 ...
- 持续更新visual studio 命令、快捷键(原创)
“格式化代码” Ctrl + E + D “注释” Ctrl + E + C “取消注释” Ctrl + E + U “点代替空白” 编辑-高级-显示空白 快捷键 Ctrl + E + S
- php 字符串是否存在
/** * 方法库-字符串是否存在 * @param string $str :字符或字符串 * @param string $string :字符串 * @return string 例子: $st ...
- 常用在线工具及API网址总结
1.小图标在线查找 https://www.iconfinder.com/ 2.在线做图,Flowchart流程图,BPMN图,Org组织结构图等 http://www.processon.com/ ...