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对话框 对话框是当前页面中弹出的一个小窗口,用于显示重要的提示信息,提示用户输入信息,确认信息,或者显示某种状态,如下载进度,退出提示等等.用户需要与对话框进行交互,才能回到原窗 ...
随机推荐
- Linux 用脚本编写搭建yum本地仓库
Linux 用脚本编写搭建yum本地仓库 源码如下: #!/bin/bash #该脚本用于自动化搭建本地yum仓库 #挂载光盘 #作者:雨中落叶 #博客:https://www.cnblogs.com ...
- ionic cordova platform add android Cordova failed to install plugin Error: ENOENT: no such file or directory AndroidManifest.xml
问题描述: 在ionic 项目中出现编译android 的时候 出现 Cordova failed to install plugin Error: ENOENT: no such file or ...
- C# 算法之选择排序
1.简介 选择排序是排序中比较简单的一种,实现的大致思路如下:首先我们拿到一个需要排序的数组,假设该数组的第一个元素是最小的,然后将数组中剩下的元素,于最小的元素进行比较,如果中间有比第一个元素的小的 ...
- PHP取得json前面有乱码(去除文件头部BOM)
curl请求接口时,返回结果如下: {} 想把json转换成数组或者对象,但是用json_decode返回是空的,然后用var_dump打印了一下返回结果,发现结果如下: ) 发现前面多了两个字符,因 ...
- Unity3D热更新之LuaFramework篇[03]--prefab加载和Button事件
在上一篇文章 Unity3D热更新之LuaFramework篇[02]--用Lua创建自己的面板 中,我介绍了LuaFramework加载面板的方法,但这个方法并不适用于其它Prefab资源,在这套框 ...
- 【ASP.NET MVC系列】浅谈ASP.NET MVC运行过程
ASP.NET MVC系列文章 [01]浅谈Google Chrome浏览器(理论篇) [02]浅谈Google Chrome浏览器(操作篇)(上) [03]浅谈Google Chrome浏览器(操作 ...
- Python异常处理详解
在shell脚本中,常用if来判断程序的某个部分是否可能会出错,并在if的分支中做出对应的处理,从而让程序更具健壮性.if判断是异常处理的一种方式,所有语言都通用.对于特性完整的编程语言来说,都有专门 ...
- Django 系列博客(六)
Django 系列博客(六) 前言 本篇博客介绍 Django 中的路由控制部分,一个网络请求首先到达的就是路由这部分,经过路由与视图层的映射关系再执行相应的代码逻辑并将结果返回给客户端. Djang ...
- SharedPreferences存储读取数据
存储 //创建SharedPreferences 存储用户名SharedPreferences sharedPreferences = getSharedPreferences("name& ...
- 人工智能第三课:数据科学中的Python
我用了两天左右的时间完成了这一门课<Introduction to Python for Data Science>的学习,之前对Python有一些基础,所以在语言层面还是比较顺利的,这门 ...