AlertDialog使用
来源:互联网 发布:拼音注音软件 编辑:程序博客网 时间:2024/05/18 00:04
可以将对话框主要分为三个部分:
上面区域是标题栏和图标
中间区域是内容区
下方是button区
其他形式各异的对话框也都是基于此的变体而已
要创建一个对话框,我们需要做些什么
1、首先需要创建一个AlertDialog.Builder对象,基本语法:
AlertDialog.Builder alertDialogBuilder=new AlertDialog.Builder(this);
2、通过调用它的create()方法就可以构造出一个对话框
AlertDialog alertDialog = alertDialogBuilder.create();alertDialog.show();//将dialog显示出来
3、通过调用alertDialogBuilder对象的setXX方法来实现
alertDialogBuilder.setTitle();//设置标题 alertDialogBuilder.setIcon();//设置图表 /*设置下方按钮*/ alertDialogBuilder.setPositiveButton(); alertDialogBuilder.setNegativeButton(); alertDialogBuilder.setNeutralButton(); /*对话框内容区域的设置提供了多种方法*/ setMessage();//设置显示文本 setItems();//设置对话框内容为简单列表项 setSingleChoiceItems();//设置对话框内容为单选列表项 setMultiChoiceItems();//设置对话框内容为多选列表项 setAdapter();//设置对话框内容为自定义列表项 setView();//设置对话框内容为自定义View //设置对话框是否可取消 setCancelable(booleab cancelable); setCancelListener(onCancelListener);
简单的AlertDialog
关键代码
private void showNormalDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.mipmap.ic_launcher); builder.setTitle(R.string.simpleDialogTitle); builder.setMessage(R.string.simpleDialogMessage); //button监听 builder.setPositiveButton(R.string.confirmButton, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(getApplicationContext(),"您点击了确定按钮",Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton(R.string.cancelButton, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(getApplicationContext(),"您点击了取消按钮",Toast.LENGTH_SHORT).show(); } }); //设置对话框是可取消的 builder.setCancelable(true); AlertDialog alertDialog = builder.create(); alertDialog.show(); }
简单列表Dialog
关键代码
private void showNormalItemDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.mipmap.ic_launcher); builder.setTitle(R.string.simpleDialogTitle); final String[] items = {"Item one","Item two","Item three"}; builder.setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(getApplicationContext(),"You clicked "+items[i],Toast.LENGTH_SHORT).show(); } }); //设置对话框是可取消的 builder.setCancelable(true); AlertDialog alertDialog = builder.create(); alertDialog.show(); }
单选列表Dialog
关键代码
private void showSingleChoiceItemDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.mipmap.ic_launcher); builder.setTitle(R.string.singleChoiceItemTitle); final String[] items = {"Item one","Item two","Item three"}; builder.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(getApplicationContext(), "You clicked "+items[i], Toast.LENGTH_SHORT).show(); } }); builder.setCancelable(true); AlertDialog alertDialog = builder.create(); alertDialog.show(); }
多选列表Dialog
关键代码
private void showMultiChoiceItemDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.mipmap.ic_launcher); builder.setTitle(R.string.multiChoiceItemTitle); final String[] items = {"Item one","Item two","Item three"}; builder.setMultiChoiceItems(items, new boolean[]{true, true, false}, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i, boolean b) { Toast.makeText(getApplicationContext(),"You clicked "+items[i]+" "+b,Toast.LENGTH_SHORT).show(); } }); builder.setCancelable(true); AlertDialog alertDialog = builder.create(); alertDialog.show(); }
自定义布局Dialog
先自定义一个布局
dialog_forward_type.xml
<?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="wrap_content" android:orientation="vertical" android:gravity="center"> <TextView android:id="@+id/dialog_forward_type_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:text="请选择转发类型"/> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#dddddd"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="20dp"> <LinearLayout android:id="@+id/dialog_forward_video_layout" android:layout_width="0dp" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center" android:layout_weight="1"> <ImageView android:layout_width="50dp" android:layout_height="50dp" android:src="@mipmap/btn_forward_video" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="视频" android:layout_marginTop="10dp"/> </LinearLayout> <LinearLayout android:id="@+id/dialog_forward_image_layout" android:layout_width="0dp" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center" android:layout_weight="1"> <ImageView android:layout_width="50dp" android:layout_height="50dp" android:src="@mipmap/btn_forward_images" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="图片" android:layout_marginTop="10dp"/> </LinearLayout> </LinearLayout></LinearLayout>
关键代码
private void showCustomDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); LinearLayout layout = (LinearLayout)getLayoutInflater().inflate(R.layout.dialog_forward_type,null); builder.setView(layout); LinearLayout videoLayout = (LinearLayout)layout.findViewById(R.id.dialog_forward_video_layout); LinearLayout imageLayout = (LinearLayout)layout.findViewById(R.id.dialog_forward_image_layout); videoLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(),"U clicked Video type",Toast.LENGTH_SHORT).show(); } }); imageLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(),"U clicked Image type",Toast.LENGTH_SHORT).show(); } }); builder.setCancelable(true); AlertDialog alertDialog = builder.create(); alertDialog.show(); }
Demo源码下载
戳这里下载源码
阅读全文
0 0
- AlertDialog使用
- AlertDialog使用
- AlertDialog使用
- AlertDialog使用
- AlertDialog使用
- AlertDialog使用
- AlertDialog使用实例
- AlertDialog使用实例
- AlertDialog使用实例
- AlertDialog类的使用
- AlertDialog 使用总结
- AlertDialog的使用
- android alertDialog的使用
- android基础AlertDialog使用
- Android AlertDialog使用
- (自定义)AlertDialog使用
- Service中使用AlertDialog
- 使用AlertDialog创建对话框
- thinkphp5.0返回上一页面
- 《Spring 5官方文档》-JMX
- Java并发之Fork-Join框架
- 2017全球女性领导者峰会暨又忙又美大赛全国总决赛将通过五个阶段的赛制和层层选拔
- Python3-json3csv
- AlertDialog使用
- 让控制台输出彩色日志 windows
- 公证书具备哪些法律效力?
- ViewPager实现循环轮播图
- Don't give me five!
- uboot的mmc命令
- homebrew命令
- Python3-Set
- 压力测试的测试点