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种的更多相关文章

  1. Android应用开发学习之AlertDialog对话框

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 本文中我们通过一个例子来看AlertDialog对话框的实现,其运行效果如下: 主布局文件main.xml内容如下: ...

  2. Android中使用AlertDialog实现几种不同的对话框

    场景 app中常见的对话框. 简单的带确定取消按钮的对话框 带列表的对话框 带单项选择的对话框 带多项选择的对话框 注: 博客: https://blog.csdn.net/badao_liumang ...

  3. android 开发 实现一个自定义布局的AlertDialog对话框

    对话框有很多实现方法,最常见的是在一个点击事件中代码直接写出对话框.如下: package com.example.lenovo.mydemo2; import android.content.Dia ...

  4. android 的AlertDialog对话框

    private int selectedFruitIndex = 0;  private void showMsg2() {//  Dialog alertDialog = new AlertDial ...

  5. 安卓 自定义AlertDialog对话框(加载提示框)

    AlertDialog有以下六种使用方法: 一.简单的AlertDialog(只显示一段简单的信息) 二.带按钮的AlertDialog(显示提示信息,让用户操作) 三.类似ListView的Aler ...

  6. AlertDialog对话框简单案例

    什么是Dialog? Dialog类,是一切对话框的基类,需要注意的是,Dialog类虽然可以在界面上显示,但是并非继承于View类,而是直接从java.lang.Object开始构造出的.类似于Ac ...

  7. 在有EditText控件的AlertDialog对话框中自动弹出输入法

    我们先回顾一下创建AlertDialog的一般步骤. 一 inflate AlertDialog的布局文件   例如,其中dlg就是我们的布局文件.    View layout = LayoutIn ...

  8. Android:AlertDialog对话框

    1.简单的ALertDialog: Dialog alertDialog = new AlertDialog.Builder(this) .setTitle("标题") .setM ...

  9. Android4.0的Alertdialog对话框,设置点击其他位置不消失

    Android4.0以上AlertDialog,包括其他自定义的dialog,在触摸对话框边缘外部,对话框消失. 可以设置这么一条属性,当然必须先AlertDialog.Builder.create( ...

随机推荐

  1. 【转载】execute、executeUpdate、executeQuery三者的区别(及返回值)

    1. ResultSet executeQuery(String sql); 执行SQL查询,并返回ResultSet 对象. 2.int executeUpdate(String sql); 可执行 ...

  2. python装饰器的构建

    #!/usr/bin/python3# -*-coding:utf-8 -*-# @Time : 2019/9/27 17:04# @Author : v_ctaozhang import funct ...

  3. 版权动态日记页脚 JS

    关键代码如下 <script>document.write(new Date().getFullYear())</script> js方法 new Date() //实例化Da ...

  4. 不要轻易使用ffmpeg的audio_device_number来设置音频设备

    最近项目中需要使用ffmpeg实现录音功能,使用的ffmpeg-3.4.4的库,根据源代码dshow.c中的定义 { "audio_device_number", "se ...

  5. 使用JedisPool资源池操作Redis,并进行性能优化

    一.使用方法 ----------------------------------------- private volatile static JedisPool pool = null; //本地 ...

  6. Design Phone Directory

    Design a Phone Directory which supports the following operations: get: Provide a number which is not ...

  7. Prefix to Infix Conversion

    Infix : An expression is called the Infix expression if the operator appears in between the operands ...

  8. [转帖]使用Grafana和Telegraf监视VMware ESXi的方法

    使用Grafana和Telegraf监视VMware ESXi的方法 2019-04-03 15:28:30作者:曾秀珠稿源:云网牛站 https://ywnz.com/linuxyffq/4660. ...

  9. http请求之of_ordering_http_post

    //Public function of_ordering_http_post (string as_vipsj,string as_url) returns string //string as_v ...

  10. Java 封装与类

    一.面向对象编程 面向对象编程三大特性:封装.继承和多态. 类是实现封装的手段,是面向对象编程的基本单元. 封装隐藏了类的内部实现细节,暴露给外界可控的操作,提高数据的完整性和安全性,提高模块的可重用 ...