效果:

1.简单对话框

AlertDialog.Builder builder=new AlertDialog.Builder(AlterDialogActivity.this);
builder
.setTitle("对话框")
.setIcon(R.mipmap.ic_launcher_round)
.setPositiveButton("确定",null)
.setNegativeButton("取消",null)
.setMessage("简单对话框")
.create()
.show();

  

2.列表对话框

        AlertDialog.Builder builder=new AlertDialog.Builder(AlterDialogActivity.this);
final String[] mItems={"item0","item1","item2","item3","item4","item5","item6"};
builder.setTitle("列表对话框")
.setItems(mItems, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(AlterDialogActivity.this,mItems[i],Toast.LENGTH_SHORT).show();
}
})
.setPositiveButton("确定",null)
.setNegativeButton("取消",null)
.create()
.show();

  

3.单选对话框

        AlertDialog.Builder builder=new AlertDialog.Builder(AlterDialogActivity.this);
final String[] items={"item1","item2","item3","item4"};
builder.setTitle("单选对话框")
.setIcon(R.mipmap.ic_launcher_round)
.setSingleChoiceItems(items,0,new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(AlterDialogActivity.this,items[i]+"被点击了",Toast.LENGTH_SHORT).show();
singleChoise=i;
}
})
.setPositiveButton("确定",null)
.setNegativeButton("取消",null)
.create()
.show();

  

4.多选对话框

        AlertDialog.Builder builder=new AlertDialog.Builder(AlterDialogActivity.this);
final String[] items={"item1","item2","item3","item4"};
builder.setTitle("多选对话框")
.setIcon(R.mipmap.ic_launcher_round)
.setMultiChoiceItems(items,new boolean[]{false,false,false,false},new DialogInterface.OnMultiChoiceClickListener(){ @Override
public void onClick(DialogInterface dialogInterface, int i, boolean b) {
if(b){
Toast.makeText(AlterDialogActivity.this,items[i]+"被选中了",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(AlterDialogActivity.this,items[i]+"被取消选中",Toast.LENGTH_SHORT).show();
}
}
})
.setPositiveButton("确定",null)
.setNegativeButton("取消",null)
.create()
.show();

  

5.自定义View对话框

        AlertDialog.Builder builder=new AlertDialog.Builder(AlterDialogActivity.this);
View view= LayoutInflater.from(this).inflate(R.layout.alterdialot_img,null);
builder
// .setTitle("自定义对话框")
// .setIcon(R.mipmap.ic_launcher_round)
.setView(view)
.setCancelable(true)
.setNegativeButton("取消",null)
.create()
.show();

  

代码:

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"> <Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click"
android:text="对话框" /> <Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click"
android:text="列表对话框" /> <Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click"
android:text="单选对话框" /> <Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click"
android:text="多选对话框" /> <Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click"
android:text="自定义对话框" />
</LinearLayout>

  activity

public class AlterDialogActivity extends AppCompatActivity {
// private AlertDialog.Builder builder;
private int singleChoise=0;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.act_alterlog);
// builder=new AlertDialog.Builder(this);
} public void click(View v){
switch (v.getId()){
case R.id.button://简单对话框
showDialog1();
break;
case R.id.button2://列表框
showDialog2();
break;
case R.id.button3://单选框
showDialog3();
break;
case R.id.button4://多选框
showDialog4();
break;
case R.id.button5://自定义contentview
showDialog5();
break;
}
} private void showDialog1() {
AlertDialog.Builder builder=new AlertDialog.Builder(AlterDialogActivity.this);
builder
.setTitle("对话框")
.setIcon(R.mipmap.ic_launcher_round)
.setPositiveButton("确定",null)
.setNegativeButton("取消",null)
.setMessage("简单对话框")
.create()
.show();
}
private void showDialog2() {
AlertDialog.Builder builder=new AlertDialog.Builder(AlterDialogActivity.this);
final String[] mItems={"item0","item1","item2","item3","item4","item5","item6"};
builder.setTitle("列表对话框")
.setItems(mItems, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(AlterDialogActivity.this,mItems[i],Toast.LENGTH_SHORT).show();
}
})
.setPositiveButton("确定",null)
.setNegativeButton("取消",null)
.create()
.show(); }
private void showDialog3() {
AlertDialog.Builder builder=new AlertDialog.Builder(AlterDialogActivity.this);
final String[] items={"item1","item2","item3","item4"};
builder.setTitle("单选对话框")
.setIcon(R.mipmap.ic_launcher_round)
.setSingleChoiceItems(items,0,new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(AlterDialogActivity.this,items[i]+"被点击了",Toast.LENGTH_SHORT).show();
singleChoise=i;
}
})
.setPositiveButton("确定",null)
.setNegativeButton("取消",null)
.create()
.show(); }
private void showDialog4() {
AlertDialog.Builder builder=new AlertDialog.Builder(AlterDialogActivity.this);
final String[] items={"item1","item2","item3","item4"};
builder.setTitle("多选对话框")
.setIcon(R.mipmap.ic_launcher_round)
.setMultiChoiceItems(items,new boolean[]{false,false,false,false},new DialogInterface.OnMultiChoiceClickListener(){ @Override
public void onClick(DialogInterface dialogInterface, int i, boolean b) {
if(b){
Toast.makeText(AlterDialogActivity.this,items[i]+"被选中了",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(AlterDialogActivity.this,items[i]+"被取消选中",Toast.LENGTH_SHORT).show();
}
}
})
.setPositiveButton("确定",null)
.setNegativeButton("取消",null)
.create()
.show(); }
private void showDialog5() {
AlertDialog.Builder builder=new AlertDialog.Builder(AlterDialogActivity.this);
View view= LayoutInflater.from(this).inflate(R.layout.alterdialot_img,null);
builder
// .setTitle("自定义对话框")
// .setIcon(R.mipmap.ic_launcher_round)
.setView(view)
.setCancelable(true)
.setNegativeButton("取消",null)
.create()
.show(); } }

  

Android之AlterDialog介绍的更多相关文章

  1. Android sdk目录介绍

    android sdk目录介绍 build-tools 各版本SDK编译工具 docs 离线开发者文档Android SDK API参考文档 extras 扩展开发包,如兼容架包. platforms ...

  2. 我的Android第三章:Android的组件介绍

    小编摘录了Android文档介绍Android四大组件的基本内容,感觉文档的内容写的很详细所以小编将它写入了博客 Android 使用Java语言开发.Android SDK 工具编译代码-以及任意数 ...

  3. GitHub上排名前100的Android开源库介绍(来自github)

    本项目主要对目前 GitHub 上排名前 100 的 Android 开源库进行简单的介绍,至于排名完全是根据 GitHub 搜索 Java 语言选择 (Best Match) 得到的结果,然后过滤了 ...

  4. android MVP模式介绍与实战

    android MVP模式介绍与实战 描述 MVP模式是什么?MVP 是从经典的模式MVC演变而来,它们的基本思想有相通的地方:Controller/Presenter负责逻辑的处理,Model提供数 ...

  5. Android 不同文件名介绍

    Android 不同文件名介绍

  6. Android发展简单介绍

    Android一词的本义指“机器人”,同一时候也是Google于2007年11月5日宣布的基于Linux平台的开源手机操作系统的名称,该平台由操作系统.中间件.用户界面和应用软件组成,号称是首个为移动 ...

  7. Android IntentService使用介绍以及源码解析

    版权声明:本文出自汪磊的博客,转载请务必注明出处. 一.IntentService概述及使用举例 IntentService内部实现机制用到了HandlerThread,如果对HandlerThrea ...

  8. Android Jetpack 组建介绍(一)——Lifecycler

    转自带你领略Android Jetpack组件的魅力 Android Jetpack 对于任何一个产品来说,我们开发中都会面对哪些问题?如:产品交互.用户体验.代码结构.数据获取.数据存储.网络优化. ...

  9. [原创]AndroBugs_Framework Android漏洞扫描器介绍

    [原创]AndroBugs_Framework Android漏洞扫描器介绍 1 AndroBugs_Framework Android 漏洞扫描器简介 一款高效的Android漏洞扫描器,可以帮助开 ...

随机推荐

  1. ubutun

    地址:http://www.cnblogs.com/dutlei/archive/2012/11/20/2778327.html

  2. loadsh学习

    由Underscore与Lodash的差异引发的思考:http://ju.outofmemory.cn/entry/106512 JavaScript工具库之Lodash Underscore和Lod ...

  3. vue参考

    https://github.com/taylorchen709/vue-admin http://element-cn.eleme.io/#/zh-CN/component/layout https ...

  4. DHCP(五)

    DHCP重新登录 在经过发现,提供,选择以及应答阶段后,DHCP客户端每次重新登录网络时,就不需要再发送DHCP DISCOVER发现信息了,而是直接发送包含前一次所分配的IP地址的DHCP REQU ...

  5. python开发IO模型:阻塞&非阻塞&异步IO&多路复用&selectors

    一 IO模型介绍 为了更好地了解IO模型,我们需要事先回顾下:同步.异步.阻塞.非阻塞 同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非 ...

  6. python学习——练习题(1)

    """ 题目:有四个数字:1.2.3.4,能组成多少个互不相同且无重复数字的三位数?各是多少? """ import itertools d ...

  7. 循序渐进Python3(十一) --4--  web之jQuery

      jQuery         jQuery是一个快速.简洁的JavaScript框架,是继Prototype之后又一个优秀的JavaScript代码库(或JavaScript框架).jQuery设 ...

  8. new Com

    CreateOleObject System.Win.ComObj.hpp #include <objbase.h>     Winapi.ActiveX.pas   CoInitiali ...

  9. 在ubuntu中安装minicom时出现devic…

    未正常关闭minicom yesaidu@ywf-ubuntu: ~$ ls /var/lock LCK..ttyS0  subsys yesaidu@ywf-ubuntu: ~$ kill 0 ye ...

  10. 03.WSDL分析

    自己做一个程序放到tomcat里面这个就是服务,自己安装一个oracle,oracle启动之后那它本身就是一种服务. WebService就是HTTP,那么它和HTTP有什么不同呢? HTTP GET ...