Android Dialog对话框实例代码讲解
作者:xixi袭阳 时间:2021-12-18 06:34:50
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对话框实例代码讲解网站的支持!
来源:https://www.cnblogs.com/94xiyang/archive/2018/08/21/9509810.html
标签:android,dialog,对话框
0
投稿
猜你喜欢
c语言定时器示例分享
2023-11-04 09:44:52
Android Jni的简单使用详解
2023-10-20 14:37:43
Java构造代码块,静态代码块原理与用法实例分析
2023-11-03 09:03:45
mvn中dependencyManagement的使用详解
2021-11-05 10:19:20
idea前后跳转箭头的快捷键
2022-09-08 10:55:44
spring boot+vue 的前后端分离与合并方案实例详解
2023-08-20 10:41:07
java开发WMS仓库商品预警需求示例解析
2023-10-03 11:07:30
Java 异步线程监听与结果回调及异常捕获总结分析
2021-10-14 01:23:40
prometheus监控springboot应用简单使用介绍详解
2023-02-24 03:49:00
旧项目升级新版Unity2021导致Visual Studio无法使用的问题
2023-12-28 21:51:26
Android上传文件到服务端并显示进度条
2023-06-23 07:48:33
Spring MVC4.1服务器端推送实现过程解析
2021-07-24 14:53:16
Android sharedPreferences实现记住密码功能
2022-09-11 13:58:37
IntelliJ IDEA中显示和关闭工具栏与目录栏的方法
2023-09-01 23:18:59
springboot如何静态加载@configurationProperties
2021-12-13 16:20:13
浅谈EventBus
2022-12-31 20:23:14
SpringBoot拦截器Filter的使用方法详解
2023-06-09 07:33:59
详解Java设计模式之备忘录模式的使用
2023-09-10 09:38:32
Java 客户端操作 FastDFS 实现文件上传下载替换删除功能
2022-06-01 15:01:38
Struts2下拉框实例解析
2021-09-16 16:29:59