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( ...
随机推荐
- 星际争霸,FF反作弊对战平台
星际一 [FF]反作弊对战平台让作弊行为无所遁形,只为星际玩家服务的反作弊对战平台目前能检查星际霸主以及其他星际争霸ZUOBI软件支持星际113版本 支持XP WIN7 WIN8 MAC 游戏外挂带来 ...
- uni-app 手指左右滑动实现翻页效果
首先给页面添加 touch 事件 <view class="text-area" @touchstart="start" @touchend=" ...
- 第二章 kali安装
@kali安装 本文以虚拟机进行安装(注意:虚拟机安装不等同于物理机安装,在虚拟机安装成功不等于一定能在物理机,U盘安装成功) 下载kali镜像 官方地址:https://www.kali.org/d ...
- 【18.065】Lecture1
由于这一课的教材放出来了,所以直接将整个pdf放上来.   
- LeetCode.1170-比较字符串中最小字符的出现频率(Compare Strings by Frequency of the Smallest Char)
这是小川的第412次更新,第444篇原创 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第263题(顺位题号是1170).在一个非空字符串s上定义一个函数f(s),该函数计算s中最小字 ...
- 李宏毅 Gradient Descent Demo 代码讲解
何为梯度下降,直白点就是,链式求导法则,不断更新变量值. 这里讲解的代码为李宏毅老师机器学习课程中 class 4 回归展示 中的代码demo Loss函数 python代码如下 import n ...
- JAVA实验报告及第七周总结
JAVA第六周作业 实验报告五 第一题 1.设计一个类层次,定义一个抽象类--形状,其中包括有求形状的面积的抽象方法. 继承该抽象类定义三角型.矩形.圆. 分别创建一个三角形.矩形.圆存对象,将各类图 ...
- rtsp学习参考资料1
转载于:http://itindex.net/detail/51966-%E6%B5%B7%E5%BA%B7-rtsp-%E5%AE%A2%E6%88%B7%E7%AB%AF 海康相机RTSP连接代码 ...
- dubbo分布式服务框架-study1
本文参考“如何给老婆解释RPC”一文进行的... 1.首先了解下dubbo: dubbo是一款高性能.轻量级的开源java RPC服务框架(RPC即远程过程调用,具体解释见:https://www.j ...
- Android Application的目录结构
目录结构: 1,java目录:保存java或kotlin源文件 2,res目录:保存Android项目的各种资源文件.比如layout子目录存放界面布局文件,values子目录存放各种XML格式的资源 ...