Android Dialog对话框
Dialog的基本方法
//创建Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
//设置标题图标
builder.setIcon(R.drawable.ic_launcher);
//设置标题
builder.setTitle("这是一个对话框");
//设置信息
builder.setMessage("是否要跳转?");
//确定按钮
setPositiveButton(CharSequence text, DialogInterface.OnClickListener listener)
//取消按钮
setNegativeButton (CharSequence text, DialogInterface.OnClickListener listener)
//忽略
setNeutralButton (CharSequence text, DialogInterface.OnClickListener listener)
//显示对话框
show();
系统样式
1.下拉列表
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("选择一个城市");
//下拉列表的数据源
final String[] cities = {"北京", "上海", "广州", "深圳", "杭州"};
builder.setItems(cities, new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which){
Toast.makeText(MainActivity.this, "你选择的城市为:" + cities[which], Toast.LENGTH_SHORT).show();
}
});
builder.show();
2.单选框列表
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("请选择性别");
final String[] sex = {"男", "女"};
//第二个参数指定默认哪个单选框被勾中
builder.setSingleChoiceItems(sex, 1, new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which){
Toast.makeText(MainActivity.this, "性别为:" + sex[which], Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which){
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which){
}
});
builder.show();
3.多选框列表
String str;AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("选择你想看的电视剧");
final String[] hobbies = {"廷禧攻略", "扶摇", "香蜜沉沉烬如霜", "如懿传"};
//第二个参数代表哪几个选项被选择,需要传递一个boolean[]数组进去,其长度要和第一个参数的长度相同,如果null表示都不选
builder.setMultiChoiceItems(hobbies, null, new DialogInterface.OnMultiChoiceClickListener(){
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked){
if(isChecked){
str.append(hobbies[which] + ", ");
}
Toast.makeText(MainActivity.this, "选择的是:" + str.toString(), Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which){
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which){
}
});
builder.show();
4.等待对话框
ProgressDialog waitingDialog= new ProgressDialog(MainActivity.this);
waitingDialog.setTitle("等待加载中,请稍后...");
waitingDialog.setMessage("等待中...");
waitingDialog.setIndeterminate(true);//采用不确定式的进度条
waitingDialog.setCancelable(false);//点击外部不取消对话框
waitingDialog.show();
5.进度条对话框
int MAXPD = 100;
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setProgress(0);//设置默认值
progressDialog.setTitle("正在下载");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//进度条样式
progressDialog.setMax(MAXPD);//设置最大值
progressDialog.show();
自定义Dialog
1.继承Dialog
public class CustomDialog extends Dialog {
//标题
protected TextView hintTv;
//左边按钮
protected Button doubleLeftBtn;
//右边按钮
protected Button doubleRightBtn;
//输入框
public EditText editText;
public CustomDialog(Context context) {
super(context, R.style.CustomDialogStyle);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setCancelable(false); // 是否可以撤销
setContentView(R.layout.dialog_custom);
hintTv = findViewById(R.id.tv_common_dialog_hint);
doubleLeftBtn = findViewById(R.id.btn_common_dialog_double_left);
doubleRightBtn = findViewById(R.id.btn_common_dialog_double_right);
editText = findViewById(R.id.et_common_dialog);
}
//置右键文字和点击事件
public void setRightButton(String rightStr, View.OnClickListener clickListener) {
doubleRightBtn.setOnClickListener(clickListener);
doubleRightBtn.setText(rightStr);
}
//设置左键文字和点击事件
public void setLeftButton(String leftStr, View.OnClickListener clickListener) {
doubleLeftBtn.setOnClickListener(clickListener);
doubleLeftBtn.setText(leftStr);
}
//设置提示内容
public void setHintText(String str) {
hintTv.setText(str);
hintTv.setVisibility(View.VISIBLE);
}
//给两个按钮 设置文字
public void setBtnText(String leftStr, String rightStr) {
doubleLeftBtn.setText(leftStr);
doubleRightBtn.setText(rightStr);
}
}
2.自定义Style
<style name="CustomDialogStyle" parent="@android:style/Theme.Dialog">
<!-- 边框 -->
<item name="android:windowFrame">@null</item>
<!-- 背景透明 -->
<item name="android:windowBackground">@color/transparent</item>
<!-- 无标题 -->
<item name="android:windowNoTitle">true</item>
<!-- 是否浮现在activity之上 -->
<item name="android:windowIsFloating">true</item>
<!-- 半透明 -->
<item name="android:windowIsTranslucent">false</item>
<!-- 背景模糊 -->
<item name="android:windowContentOverlay">@null</item>
<!-- 允许对话框的背景变暗 -->
<item name="android:backgroundDimEnabled">true</item>
<!-- 字体颜色 -->
<item name="android:textColor">@color/white</item>
<item name="android:editTextColor">@color/white</item>
</style>
3.自定义布局
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ll_common_dialog_layout"
android:layout_width="500dp"
android:layout_height="250dp"
android:layout_margin="5dp"
android:background="@drawable/background_info"
android:orientation="vertical"
android:gravity="center">
<!--提示信息-->
<TextView
android:id="@+id/tv_common_dialog_hint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@color/white"
android:textSize="27sp"/>
<EditText
android:id="@+id/et_common_dialog"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:textColor="@color/back"
android:inputType="text|textNoSuggestions"
tools:ignore="LabelFor"
android:hint="请输入密码"/>
<!--底部按钮-->
<LinearLayout
android:id="@+id/ll_common_dialog_double"
android:layout_width="360dp"
android:layout_height="60dp"
android:layout_margin="20dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_common_dialog_double_left"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/btnselector"
android:gravity="center"
android:textColor="@color/white"
android:textSize="27dp"/>
<Button
android:id="@+id/btn_common_dialog_double_right"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/btnselector"
android:gravity="center"
android:textColor="@color/white"
android:textSize="27dp"/>
</LinearLayout>
</LinearLayout>
4.ipad隐藏底部虚拟按键
//弹出dialog时隐藏底部虚拟按键
dialog.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
dialog.getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
int uiOptions =View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
if (Build.VERSION.SDK_INT >= 19) {
uiOptions |= 0x00001000;
} else {
uiOptions |= View.SYSTEM_UI_FLAG_LOW_PROFILE;
}
dialog.getWindow().getDecorView().setSystemUiVisibility(uiOptions);
}
});
5.使用自定义Dialog
CustomDialog dialog = new CustomDialog(this);
dialog.setHintText("请输入密码");
dialog.setLeftButton("取消", new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.setRightButton("确定", new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
dialog.show();
Android Dialog对话框的更多相关文章
- Android Dialog对话框的七种形式的使用
参考资料:http://www.oschina.net/question/54100_32486 注:代码进行了整理 在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询 ...
- Android Dialog使用举例
在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,在我们使用Android的过程中,我归纳了一 ...
- Android控件——7种形式的Android Dialog使用举例(转载)
在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,在我们使用Android的过程中,我归纳了一 ...
- 8种形式的Android Dialog使用举例
在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,在我们使用Android的过程中,我归纳了一 ...
- Android UI系列-----Dialog对话框
您可以通过点击 右下角 的按钮 来对文章内容作出评价, 也可以通过左下方的 关注按钮 来关注我的博客的最新动态. 如果文章内容对您有帮助, 不要忘记点击右下角的 推荐按钮 来支持一下哦 如果您对文章内 ...
- Android中制作自定义dialog对话框的实例
http://www.jb51.net/article/83319.htm 这篇文章主要介绍了Android中制作自定义dialog对话框的实例分享,安卓自带的Dialog显然不够用,因而我们要继 ...
- android从Dialog对话框中取得文本文字
android中Dialog对话框获取文本文字,只需要使用editor的getText方法就可以获得,示例如下:final EditText et = new EditText(this); et.s ...
- Android之UI--打造12种Dialog对话框
最近有空,来把app中常用到的Dialog对话框写一篇博客,在app中很多地方都会用到Dialog对话框,今天小编我就给大家介绍Dialog对话框. 先看看效果图: 12种,可根据需求选择,上图可知, ...
- Android学习(十九)Dialog对话框
一.什么是Dialog对话框 对话框是当前页面中弹出的一个小窗口,用于显示重要的提示信息,提示用户输入信息,确认信息,或者显示某种状态,如下载进度,退出提示等等.用户需要与对话框进行交互,才能回到原窗 ...
随机推荐
- JAVA面试题-数组字符串基础
1.大写的Integer和String是可变类还是不可变类?怎么定义不可变类?不可变.用final关键字,如public final class Integer extends Number 2.比较 ...
- linux的tar命令
Linux下的tar压缩解压缩命令详解 tar -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中 ...
- sql server 性能调优之 资源等待 LCk
一. 概述 这次介绍实例级别资源等待LCK类型锁的等待时间,关于LCK锁的介绍可参考 “sql server 锁与事务拨云见日”.下面还是使用sys.dm_os_wait_stats 来查看,并找出 ...
- Android自动化测试之MonkeyRunner使用
MonkeyRunner工具是使用Jython(使用Java编程语言实现的Python)写出来的,它提供了多个API,通过monkeyrunner API 可以写一个Python的程序来模拟操作控制A ...
- C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...
- java泛型(二)、泛型的内部原理:类型擦除以及类型擦除带来的问题
微信公众号[程序员江湖] 作者黄小斜,斜杠青年,某985硕士,阿里 Java 研发工程师,于 2018 年秋招拿到 BAT 头条.网易.滴滴等 8 个大厂 offer,目前致力于分享这几年的学习经验. ...
- springboot Aop 统一处理Web请求日志
1.增加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- iOS逆向开发(5):微信强制升级的突破 | 多开 | 微信5.0
接下来的几篇文章,小程以微信为例,实战地演示一下:如何注入iOS的APP.其中使用到的知识,基本在前面的文章中都有介绍到. 小白:小程,我想用回旧版本的微信! 小程:为什么要用旧版本微信呢? 小白:你 ...
- C++ STL中的map用红黑树实现,搜索效率是O(lgN),为什么不像python一样用散列表从而获得常数级搜索效率呢?
C++ STL中的标准规定: map, 有序 unordered_map,无序,这个就是用散列表实现 谈谈hashmap和map的区别,我们知道hashmap是平均O(1),map是平均O(lnN)的 ...
- 翻译:insert on duplicate key update(已提交到MariaDB官方手册)
本文为mariadb官方手册:INSERT ON DUPLICATE KEY UPDATE的译文. 原文:https://mariadb.com/kb/en/insert-on-duplicate-k ...