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对话框 对话框是当前页面中弹出的一个小窗口,用于显示重要的提示信息,提示用户输入信息,确认信息,或者显示某种状态,如下载进度,退出提示等等.用户需要与对话框进行交互,才能回到原窗 ...
随机推荐
- Spark面试题
RDD怎么理解? RDD 是 Spark 的灵魂,也称为弹性分布式数据集.一个 RDD 代表一个可以被分区的只读数据集.RDD 内部可以有许多分区(partitions),每个分区又拥有大量的记录(r ...
- @vue/cli 构建得项目eslint配置
如下:package.json // package.json { "name": "ecommerce-mall-front", "version& ...
- 用dos命令导出一个文件夹里面所有文件的名字(装逼利器)
首先,当然是在相关的文件夹打开dos命令窗口. 然后,输入如下命令:dir/b >a.txt 如果你非常了解dos命令,那么你一定会觉得这个东西简单到爆,而且我的理解和猜想都有些无知. 但如果你 ...
- [NewLife.XCode]脏数据
NewLife.XCode是一个有10多年历史的开源数据中间件,支持nfx/netstandard,由新生命团队(2002~2019)开发完成并维护至今,以下简称XCode. 整个系列教程会大量结合示 ...
- Zookeeper Client简介
直接使用zk的api实现业务功能比较繁琐.因为要处理session loss,session expire等异常,在发生这些异常后进行重连.又因为ZK的watcher是一次性的,如果要基于wather ...
- spring面试问题与答案集锦
我收集了一些spring面试的问题,这些问题可能会在下一次技术面试中遇到.对于其他spring模块,我将单独分享面试问题和答案. 如果你能将在以前面试中碰到的,且你认为这些应该是一个有spring经验 ...
- Go基础系列:互斥锁Mutex和读写锁RWMutex用法详述
sync.Mutex Go中使用sync.Mutex类型实现mutex(排他锁.互斥锁).在源代码的sync/mutex.go文件中,有如下定义: // A Mutex is a mutual exc ...
- Video for Linux Two API Specification
V4L2 的使用规范,网址为:https://www.linuxtv.org/downloads/legacy/video4linux/API/V4L2_API/spec-single/v4l2.ht ...
- MySQL系列详解十:MySQL多源复制演示-技术流ken
前言 多源复制即多主一从结构,多个主服务器端的数据都会同步到后端一个从服务器上面.至于为什么要做多源复制下面的总结很到位. 1.灾备作用:将各个库汇总在一起,就算是其他库都挂了(整个机房都无法连接了) ...
- IDEA与Eclipse
IDEA 1 快捷键 快速查找某个类 double shift 显示类结构图 ctrl+H 代码上移或下移 ctrl+shift+up/down 查找文件 ctrl+shift+N 删除当前行 ctr ...