AlertDialog 对话框 5种

MainActivity.class
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button bt1;
private Button bt2;
private Button bt3;
private Button bt4;
private Button bt5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1 = (Button) findViewById(R.id.bt1);
bt1.setOnClickListener(this);
bt2 = (Button) findViewById(R.id.bt2);
bt2.setOnClickListener(this);
bt3 = (Button) findViewById(R.id.bt3);
bt3.setOnClickListener(this);
bt4 = (Button) findViewById(R.id.bt4);
bt4.setOnClickListener(this);
bt5 = (Button) findViewById(R.id.bt5);
bt5.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
//确认对话框
case R.id.bt1:{
Log.d("xys", "进入了");
//创建Builder构建器
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("确认对话框!");//设置标题
builder.setIcon(R.mipmap.ic_launcher);//设置图标
builder.setMessage("确认对话框内容。");//设置文本内容
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "这里是取消按钮!", Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "这里是确定 按钮!", Toast.LENGTH_SHORT).show();
}
});
//创建出dialog并show出来
// AlertDialog dialog = builder.create();
// dialog.show();
builder.create().show();
break;
}
//单选按钮对话框
case R.id.bt2:{
final String[] list = {"男","女","女博士","程序员"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("选择性别");//设置标题
builder.setIcon(R.mipmap.ic_launcher);//设置图标
//数据源,默认选中的项目
builder.setSingleChoiceItems(list, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你选择的是" + list[which] + "这一项", Toast.LENGTH_SHORT).show();
}
});
builder.create().show();
break;
}
//多选按钮对话框
case R.id.bt3:{
final String[] list = {"口琴","吉他","电脑","跳水"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("爱好");
builder.setIcon(R.mipmap.ic_launcher);
builder.setMultiChoiceItems(list, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
Toast.makeText(MainActivity.this, "我的爱好是" + list[which], Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "关我屁事", Toast.LENGTH_SHORT).show();
}
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.create().show();
break;
}
//列表对话框
case R.id.bt4:{
final String [] list ={"宣传","策划","打酱油","捣糨糊"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("爱好");
builder.setIcon(R.mipmap.ic_launcher);
builder.setItems(list, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你选择的部门是" + list[which], Toast.LENGTH_SHORT).show();
}
});
builder.create().show();
break;
}
//自定义对话框
case R.id.bt5:{
final String [] list ={"宣传","策划","打酱油","捣糨糊"};
//获取自定义布局
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.dialog_layout, null);
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(view);
builder.setTitle("自定义对话框");
//监听自定义按钮
final EditText text = (EditText) view.findViewById(R.id.layout_text);
Button bt = (Button) view.findViewById(R.id.layout_bt1);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String str = text.getText().toString();
Toast.makeText(MainActivity.this, "提交的内容是 " + str, Toast.LENGTH_SHORT).show();
}
});
final AlertDialog dialog = builder.create();
dialog.show();
// builder.create().show();
break;
}
}
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <Button
android:id="@+id/bt1"
android:text="确认对话框"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <Button
android:id="@+id/bt2"
android:layout_below="@id/bt1"
android:text="单选对话框"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <Button
android:id="@+id/bt3"
android:layout_below="@id/bt2"
android:text="多选对话框"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <Button
android:id="@+id/bt4"
android:layout_below="@id/bt3"
android:text="列表对话框"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <Button
android:id="@+id/bt5"
android:layout_below="@id/bt4"
android:text="自定义对话框"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> </RelativeLayout>
dialog_layout.xml(自定义布局)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"> <EditText
android:id="@+id/layout_text"
android:hint="请输入内容"
android:layout_weight="5"
android:layout_width="0dp"
android:layout_height="wrap_content" /> <Button
android:id="@+id/layout_bt1"
android:layout_weight="1.5"
android:text="提交"
android:layout_width="0dp"
android:layout_height="wrap_content" /> </LinearLayout>
<ImageView
android:id="@+id/layout_image"
android:src="@drawable/abc"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

AlertDialog 对话框 5种的更多相关文章
- Android应用开发学习之AlertDialog对话框
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 本文中我们通过一个例子来看AlertDialog对话框的实现,其运行效果如下: 主布局文件main.xml内容如下: ...
- Android中使用AlertDialog实现几种不同的对话框
场景 app中常见的对话框. 简单的带确定取消按钮的对话框 带列表的对话框 带单项选择的对话框 带多项选择的对话框 注: 博客: https://blog.csdn.net/badao_liumang ...
- android 开发 实现一个自定义布局的AlertDialog对话框
对话框有很多实现方法,最常见的是在一个点击事件中代码直接写出对话框.如下: package com.example.lenovo.mydemo2; import android.content.Dia ...
- android 的AlertDialog对话框
private int selectedFruitIndex = 0; private void showMsg2() {// Dialog alertDialog = new AlertDial ...
- 安卓 自定义AlertDialog对话框(加载提示框)
AlertDialog有以下六种使用方法: 一.简单的AlertDialog(只显示一段简单的信息) 二.带按钮的AlertDialog(显示提示信息,让用户操作) 三.类似ListView的Aler ...
- AlertDialog对话框简单案例
什么是Dialog? Dialog类,是一切对话框的基类,需要注意的是,Dialog类虽然可以在界面上显示,但是并非继承于View类,而是直接从java.lang.Object开始构造出的.类似于Ac ...
- 在有EditText控件的AlertDialog对话框中自动弹出输入法
我们先回顾一下创建AlertDialog的一般步骤. 一 inflate AlertDialog的布局文件 例如,其中dlg就是我们的布局文件. View layout = LayoutIn ...
- Android:AlertDialog对话框
1.简单的ALertDialog: Dialog alertDialog = new AlertDialog.Builder(this) .setTitle("标题") .setM ...
- Android4.0的Alertdialog对话框,设置点击其他位置不消失
Android4.0以上AlertDialog,包括其他自定义的dialog,在触摸对话框边缘外部,对话框消失. 可以设置这么一条属性,当然必须先AlertDialog.Builder.create( ...
随机推荐
- 树莓派-CentOS-Minimal arm版的设置
将镜像用 balenaEtcher 写入到树莓派SD卡并启动后,需要对其进行一些设置才能正常使用. 1. 用户名 root 密码 centos 2. 扩展 rootfs 到最大可用空间:cat REA ...
- Git(3):分支管理
Git 分支管理 几乎每一种版本控制系统都以某种形式支持分支.使用分支意味着你可以从开发主线上分离开来,然后在不影响主线的同时继续工作. 创建分支命令 $git branch <branch n ...
- 禁止crontab -r清空定时任务列表误操作
关于crontab 1.附件cron.sh放/usr/bin目录下面 2.在/etc/bashrc文件末尾添加 alias crontab='/usr/bin/cron.sh' 或者在当前root用户 ...
- JAVA是否适合非科班者自学入行?石油工程专业从培训到JAVA入门自学亲身经历
如今的我已经过了三十而立的年纪,虽然在三十岁我没有立下任何事业,相反,还在茫茫苦海中挣扎. 但是我并不是没有收获.当然,曾经在我拥有大好青春年华的时候选择了迷茫,以至于当我有所明悟的时候,却已经错过了 ...
- PJzhang:python基础进阶的10个疗程-one
猫宁!!! 课程导学 北京理工大学 国家精品在线开放课程 零基础.大学水平 100行左右的python可以做很多事情 编程是基本技能,体会思维 时间成本和收益的关系 每周5个小时 https://py ...
- LCA -cogs2098 [SYOI 2015] Asm.Def的病毒
题目链接:http://cogs.pro:8081/cogs/problem/problem.php?pid=vQXmxVaPU [题目描述] “这就是我们最新研制的,世界上第一种可持久化动态计算机病 ...
- IE11报错:[vuex] vuex requires a Promise polyfill in this browser的问题解决
昨天其他同事反馈IE浏览器无法打开线上的应用,查看了一下,发现控制台报以下错误: 发现和vuex有关系,去其官网查看了一下文档,发现关于IE浏览器的Promise的问题: 在vue-cli(webpa ...
- day22 subprocess、configeparser、表格操作模块
今日内容: 1.configparser模块的使用 2.subprocess模块的使用 3.表格处理模块 xlrd模块 xlwt模块 1.configparser模块 configparser模块是用 ...
- mysql --explain+slowlog
一.EXPALIN 在SQL语句之前加上EXPLAIN关键字就可以获取这条SQL语句执行的计划 那么返回的这些字段是什么呢? 我们先关心一下比较重要的几个字段: 1. select_type 查询类型 ...
- 【Python】【demo实验18】【练习实例】【统计输入字符串中,数字的个数、英文字母的个数及其他符号的个数】
原题: 输入一行字符,分别统计出其中英文字母.空格.数字和其它字符的个数. (本题暂时不支持中文字符及汉字) 我的代码: #!/usr/bin/python # encoding=utf-8 # -* ...