Android 常见对话框的简单使用(提示信息对话框、单选多选对话框、自定义对话框)
目录
一、提示信息对话框:
//显示提示消息对话框
private void showMsgDialog() {
//创建AlertDialog构造器Builder对象,AlertDialog建议使用android.support.v7.app包下的。
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//设置对话框标题
builder.setTitle("提示信息对话框");
//设置提示信息
builder.setMessage("是否确定退出!");
//设置对话框图标
builder.setIcon(R.mipmap.ic_launcher);
//添加确定按钮
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//添加确定按钮点击的处理代码
Toast.makeText(MainActivity.this, "点击了确定!", Toast.LENGTH_SHORT).show();
}
});
//添加取消按钮
builder.setNegativeButton("取消",null);
//创建并显示对话框
builder.show();
}
二、单选对话框:
//显示单选对话框
private void showSingleChoiceDialog() {
//创建AlertDialog构造器Builder对象,AlertDialog建议使用android.support.v7.app包下的。
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//设置对话框标题
builder.setTitle("请选择性别");
//设置对话框图标
builder.setIcon(R.mipmap.ic_launcher);
final String[] sexs = new String[]{"男", "女"};
//设置单选选项
builder.setSingleChoiceItems(sexs, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "您选择了:"+sexs[which], Toast.LENGTH_SHORT).show();
}
});
//添加确定按钮
builder.setPositiveButton("确定", null);
//创建并显示对话框
builder.show();
}
三、多选对话框:
//显示多选对话框
private void showMultiChoiceDialog() {
//创建AlertDialog构造器Builder对象,AlertDialog建议使用android.support.v7.app包下的。
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//设置对话框标题
builder.setTitle("请选择传感器");
//设置对话框图标
builder.setIcon(R.mipmap.ic_launcher);
final String[] sensors = new String[]{"温湿度传感器", "光照传感器","CO2传感器","风速传感器"};
//设置多选选项
builder.setMultiChoiceItems(sensors, new boolean[]{false,true,true,false}, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
}
});
//添加确定按钮
builder.setPositiveButton("确定", null);
//创建并显示对话框
builder.show();
}
四、自定义对话框:
自定义对话框布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#2372c1"
android:gravity="center"
android:text="提示"
android:padding="5dp"
android:textColor="#fff"
android:textSize="25sp" />
<TextView
android:id="@+id/tvContent"
android:layout_width="match_parent"
android:layout_height="200dp"
android:gravity="center"
android:text="自定义对话框内容" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#c0c0c0"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/btnOk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定" />
<Button
android:id="@+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="取消" />
</LinearLayout>
</LinearLayout>
MyDialog.Java
package com.newland.dialogdemo;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
public class MyDialog extends Dialog {
private String title;
private String content;
private TextView tvTitle;
private TextView tvContent;
private Button btnOk;
private Button btnCancel;
public MyDialog(Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//去除标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
//引入自定义对话框布局
setContentView(R.layout.my_dialog);
//初始化控件
initView();
//设置标题
tvTitle.setText(title);
//设置内容
tvContent.setText(content);
//注册确认按钮监听器
btnOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//点击确认时的操作
}
});
//注册取消按钮监听器
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//关闭对话框
dismiss();
}
});
}
//初始化控件
private void initView() {
tvTitle = findViewById(R.id.tvTitle);
tvContent = findViewById(R.id.tvContent);
btnOk = findViewById(R.id.btnOk);
btnCancel = findViewById(R.id.btnCancel);
}
public void setTitle(String title) {
this.title = title;
}
public void setContent(String content) {
this.content = content;
}
}
MainActivity.Java
//显示自定义对话框
private void showCustomDialog() {
MyDialog dialog = new MyDialog(this);
dialog.setTitle("自定义对话框");
dialog.setContent("你好!这里是自定义对话框!");
dialog.show();
}
演示项目完整代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btnShowMsgDlg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提示信息对话框"/>
<Button
android:id="@+id/btnShowSingleDlg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="单选对话框"/>
<Button
android:id="@+id/btnShowMultiDlg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="多选对话框"/>
<Button
android:id="@+id/btnShowCustomDlg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义对话框"/>
</LinearLayout>
package com.newland.dialogdemo;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//显示提示信息对话框按钮
private Button btnShowMsgDlg;
//显示单选对话框按钮
private Button btnShowSingleDlg;
//显示多选对话框按钮
private Button btnShowMultiDlg;
//显示自定义对话框按钮
private Button btnShowCustomDlg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化视图以及监听器
initView();
}
//初始化视图以及监听器
private void initView() {
//初始化控件
btnShowMsgDlg = findViewById(R.id.btnShowMsgDlg);
btnShowSingleDlg = findViewById(R.id.btnShowSingleDlg);
btnShowMultiDlg = findViewById(R.id.btnShowMultiDlg);
btnShowCustomDlg = findViewById(R.id.btnShowCustomDlg);
//注册按钮监听器
btnShowMsgDlg.setOnClickListener(this);
btnShowSingleDlg.setOnClickListener(this);
btnShowMultiDlg.setOnClickListener(this);
btnShowCustomDlg.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnShowMsgDlg: //显示提示信息对话框按钮
showMsgDialog();
break;
case R.id.btnShowSingleDlg: //显示单选对话框按钮
showSingleChoiceDialog();
break;
case R.id.btnShowMultiDlg: //显示多选对话框按钮
showMultiChoiceDialog();
break;
case R.id.btnShowCustomDlg: //显示自定义对话框按钮
showCustomDialog();
break;
}
}
//显示提示消息对话框
private void showMsgDialog() {
//创建AlertDialog构造器Builder对象,AlertDialog建议使用android.support.v7.app包下的。
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//设置对话框标题
builder.setTitle("提示信息对话框");
//设置提示信息
builder.setMessage("是否确定退出!");
//设置对话框图标
builder.setIcon(R.mipmap.ic_launcher);
//添加确定按钮
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//添加确定按钮点击的处理代码
Toast.makeText(MainActivity.this, "点击了确定!", Toast.LENGTH_SHORT).show();
}
});
//添加取消按钮
builder.setNegativeButton("取消",null);
//创建并显示对话框
builder.show();
}
//显示单选对话框
private void showSingleChoiceDialog() {
//创建AlertDialog构造器Builder对象,AlertDialog建议使用android.support.v7.app包下的。
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//设置对话框标题
builder.setTitle("请选择性别");
//设置对话框图标
builder.setIcon(R.mipmap.ic_launcher);
final String[] sexs = new String[]{"男", "女"};
//设置单选选项
builder.setSingleChoiceItems(sexs, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "您选择了:"+sexs[which], Toast.LENGTH_SHORT).show();
}
});
//添加确定按钮
builder.setPositiveButton("确定", null);
//创建并显示对话框
builder.show();
}
//显示多选对话框
private void showMultiChoiceDialog() {
//创建AlertDialog构造器Builder对象,AlertDialog建议使用android.support.v7.app包下的。
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//设置对话框标题
builder.setTitle("请选择传感器");
//设置对话框图标
builder.setIcon(R.mipmap.ic_launcher);
final String[] sensors = new String[]{"温湿度传感器", "光照传感器","CO2传感器","风速传感器"};
//设置多选选项
builder.setMultiChoiceItems(sensors, new boolean[]{false,true,true,false}, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
}
});
//添加确定按钮
builder.setPositiveButton("确定", null);
//创建并显示对话框
builder.show();
}
//显示自定义对话框
private void showCustomDialog() {
MyDialog dialog = new MyDialog(this);
dialog.setTitle("自定义对话框");
dialog.setContent("你好!这里是自定义对话框!");
dialog.show();
}
}
Android 常见对话框的简单使用(提示信息对话框、单选多选对话框、自定义对话框)的更多相关文章
- Android自定义组件系列【13】——Android自定义对话框如此简单
在我们的日常项目中很多地方会用到对话框,但是Android系统为我们提供的对话框样子和我们精心设计的界面很不协调,在这种情况下我们想很自由的定义对话框,或者有的时候我们的对话框是一个图片,没有标题和按 ...
- Android Studio常见对话框(普通对话框、单选对话框、多选对话框、进度条对话框、消息对话框、自定义对话框)
Android Studio常见对话框(普通对话框.单选对话框.多选对话框.进度条对话框.消息对话框.自定义对话框) 1.普通对话框 2.单选对话框 3.多选对话框 4.进度条对话框 5.消息对话框 ...
- Android 自定义对话框
Android实现自定义对话框效果: 核心代码: package com.example.diydialog; import android.os.Bundle; import android.app ...
- android继承Dialog实现自定义对话框
有时需要自定义对话框,可以使用AlterDialog.Bulider,比如下面的代码片段 new AlertDialog.Builder(self) .setTitle("标题") ...
- Android开发 ---构建对话框Builder对象,消息提示框、列表对话框、单选提示框、多选提示框、日期/时间对话框、进度条对话框、自定义对话框、投影
效果图: 1.activity_main.xml 描述: a.定义了一个消息提示框按钮 点击按钮弹出消息 b.定义了一个选择城市的输入框 点击按钮选择城市 c.定义了一个单选提示框按钮 点击按钮选择某 ...
- Android开发学习笔记-自定义对话框
系统默认的对话框只能显示简单的标题内容以及按钮,而如果想要多现实其他内容则就需要自定义对话框,下面是自定义对话框的方法. 1.先定义对话框的模版 <?xml version="1.0& ...
- Android—关于自定义对话框的工具类
开发中有很多地方会用到自定义对话框,为了避免不必要的城府代码,在此总结出一个工具类. 弹出对话框的地方很多,但是都大同小异,不同无非就是提示内容或者图片不同,下面这个类是将提示内容和图片放到了自定义函 ...
- Android中的AlertDialog使用示例五(自定义对话框)
在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,AlertDialog实现方法为建造者模式. ...
- Android自定义对话框
在android中有自带的对话框,为了美观,很多开发者会使用自定义对话框,如下图: 点击“弹出自定义对话框按钮后”显示如图效果. 首先要自己定义一个xml文件定义自己对话框的样式: <?xml ...
随机推荐
- 100个Shell脚本—【脚本6】拷贝目录
[脚本6]拷贝目录 编写shell脚本,把/root/目录下的所有目录(只需要一级)拷贝到/tmp/目录下: 一.脚本 #!/bin/bash cd /root list=(`ls`) for i i ...
- Linux 设置时区
一.查看和修改Linux的时区 1. 查看当前时区命令 : "date -R" 2. 修改设置Linux服务器时区方法 A命令 : "tzselect" 方法 ...
- 远程连接mysql库问题
如果你想连接你的mysql的时候发生这个错误: ERROR 1130: Host '192.168.1.3' is not allowed to connect to this MySQL serve ...
- Alamofire-5.0.0 以上报错
摘要 Alamofire 更新到新版本时,遇到了两个错误和一个警告️,所以记录下来它们,以及如何解决它们.给其他出现类似问题的同道一些解决的方向. 今天新开启一个项目,因为网络请求选择 Alamofi ...
- C/C++ Qt 数据库与Chart历史数据展示
在前面的博文中具体介绍了QChart组件是如何绘制各种通用的二维图形的,本章内容将继续延申一个新的知识点,通过数据库存储某一段时间节点数据的走向,当用户通过编辑框提交查询记录时,程序自动过滤出该时间节 ...
- 培训班输出的大量学员,会对IT行业产生哪些影响?
先说下会有哪些影响呢? 1 可能也就是些大城市的,规模比较大的,口碑比较好的培训学校输出的码农才能入行,而且能做长久.一些线上的所谓培训机构,或者小城市的培训学校,输出的能入行的码农,其实规模很有 ...
- 启动Springboot 报错 Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Sat Jan 12 15:50:25 CST 2019 There was an unexpected error (type=Not
解决方案:http://www.cnblogs.com/michaelShao/p/6675186.html
- 记ByteCTF中的Node题
记ByteCTF中的Node题 我总觉得字节是跟Node过不去了,初赛和决赛都整了个Node题目,当然PHP.Java都是必不可少的,只是我觉得Node类型的比较少见,所以感觉挺新鲜的. Nothin ...
- [BUUCTF]REVERSE——[BJDCTF2020]easy
[BJDCTF2020]easy 附件 例行检查,无壳,32位程序 32位ida载入,main函数和字符串理都没有找到有关flag的提示 根据main函数的提示,有关flag的函数应该被藏起来了,在左 ...
- [BUUCTF]PWN——bjdctf_2020_babyrop2
bjdctf_2020_babyrop2 附件 步骤: 例行检查,64位程序,开启了NX和canary保护 2. 试运行一下程序,看看大概的情况 提示我们去泄露libc 3. 64位ida载入,从ma ...