Android中的对话框形式大致可分为五种:分别是一般对话框形式,列表对话框形式,单选按钮对话框,多选按钮对话框,自定义对话框。
在实际开发中,用系统的对话框会很少,因为太丑了,美工不愿意,多是使用自定义对话框。当然学会系统的,自定义就简单了,所以我们先来学习系统的,后面在写一篇自定义对话框。
一般对话框:
不多说先上图:
代码:
- private void dialog1(){
- AlertDialog.Builder builder=new AlertDialog.Builder(this); //先得到构造器
- builder.setTitle("提示"); //设置标题
- builder.setMessage("是否确认退出?"); //设置内容
- builder.setIcon(R.mipmap.ic_launcher);//设置图标,图片id即可
- builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { //设置确定按钮
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss(); //关闭dialog
- Toast.makeText(MainActivity.this, "确认" + which, Toast.LENGTH_SHORT).show();
- }
- });
- builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { //设置取消按钮
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- Toast.makeText(MainActivity.this, "取消" + which, Toast.LENGTH_SHORT).show();
- }
- });
- builder.setNeutralButton("忽略", new DialogInterface.OnClickListener() {//设置忽略按钮
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- Toast.makeText(MainActivity.this, "忽略" + which, Toast.LENGTH_SHORT).show();
- }
- });
- //参数都设置完成了,创建并显示出来
- builder.create().show();
- }
是不是觉得三个按钮写了三遍响应事件,很繁琐呢?注意:onClick的参数中有一个which,这个是什么意思呢?这个which实际上代表的是一个唯一的int型数值。像上面的setPositiveButton中的which代表的是-1,setNegativeButton中的which代表的是-3,setNeutralButton中的which代表的是-2. 到了这里相信大家已经想到怎么简洁的写法了,只要写一个响应事件,用which参数去区分是那个按钮就可以了!
看简洁的代码2:和上面的效果是一样的!
- private void dialog1_1(){
- //先new出一个监听器,设置好监听
- DialogInterface.OnClickListener dialogOnclicListener=new DialogInterface.OnClickListener(){
- @Override
- public void onClick(DialogInterface dialog, int which) {
- switch(which){
- case Dialog.BUTTON_POSITIVE:
- Toast.makeText(MainActivity.this, "确认" + which, Toast.LENGTH_SHORT).show();
- break;
- case Dialog.BUTTON_NEGATIVE:
- Toast.makeText(MainActivity.this, "取消" + which, Toast.LENGTH_SHORT).show();
- break;
- case Dialog.BUTTON_NEUTRAL:
- Toast.makeText(MainActivity.this, "忽略" + which, Toast.LENGTH_SHORT).show();
- break;
- }
- }
- };
- //dialog参数设置
- AlertDialog.Builder builder=new AlertDialog.Builder(this); //先得到构造器
- builder.setTitle("提示"); //设置标题
- builder.setMessage("是否确认退出?"); //设置内容
- builder.setIcon(R.mipmap.ic_launcher);//设置图标,图片id即可
- builder.setPositiveButton("确认",dialogOnclicListener);
- builder.setNegativeButton("取消", dialogOnclicListener);
- builder.setNeutralButton("忽略", dialogOnclicListener);
- builder.create().show();
- }
列表对话框:
代码:
- private void dialog2() {
- final String items[]={"张三","李四","王五"};
- //dialog参数设置
- AlertDialog.Builder builder=new AlertDialog.Builder(this); //先得到构造器
- builder.setTitle("提示"); //设置标题
- //builder.setMessage("是否确认退出?"); //设置内容
- builder.setIcon(R.mipmap.ic_launcher);//设置图标,图片id即可
- //设置列表显示,注意设置了列表显示就不要设置builder.setMessage()了,否则列表不起作用。
- builder.setCancelable(false);//设置是否可以被取消(就是点击对话框范围外的地方是否取消对话框)
- builder.setItems(items,new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- Toast.makeText(MainActivity.this, items[which], Toast.LENGTH_SHORT).show();
- }
- });
- builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
- }
- });
- builder.create().show();
- }
说明:列表对话框只需设置Items属性即可,注意不能在设置Message属性,否则只会显示Message,不会显示列表。这里的setItems中的Onclick中的which属性是items数组的下标!
单选按钮对话框:
代码:
- private void dialog3(){
- final String items[]={"男","女"};
- AlertDialog.Builder builder=new AlertDialog.Builder(this); //先得到构造器
- builder.setTitle("提示"); //设置标题
- builder.setIcon(R.mipmap.ic_launcher);//设置图标,图片id即可
- builder.setSingleChoiceItems(items,0,new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- //dialog.dismiss();
- Toast.makeText(MainActivity.this, items[which], Toast.LENGTH_SHORT).show();
- }
- });
- builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
- }
- });
- builder.create().show();
- }
说明:其实也没什么好说的,和上面的差不多,只是设置单选用setSingleChoiceItems,注意这里的参数:items是显示的文本,0表示默认选中是第一个,如图所示是默认选中“男”。
多选列表对话框:
代码:
- private void dialog4(){
- final String items[]={"篮球","足球","排球"};
- final boolean selected[]={true,false,true};
- AlertDialog.Builder builder=new AlertDialog.Builder(this); //先得到构造器
- builder.setTitle("提示"); //设置标题
- builder.setIcon(R.mipmap.ic_launcher);//设置图标,图片id即可
- builder.setMultiChoiceItems(items,selected,new DialogInterface.OnMultiChoiceClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which, boolean isChecked) {
- // dialog.dismiss();
- Toast.makeText(MainActivity.this, items[which]+isChecked, Toast.LENGTH_SHORT).show();
- }
- });
- builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
- //android会自动根据你选择的改变selected数组的值。
- for (int i=0;i<selected.length;i++){
- Log.e("hongliang",""+selected[i]);
- }
- }
- });
- builder.create().show();
- }
说明:setMultiChoiceItems中的参数:selected是默认的对应的选中状态。当你选择时,系统会自动帮你把selected中的值做相应改变,所以在确定按钮中可以得到所有的选择状态。其他和单选一样。
总结:要想更好的使用对话框,自定义对话框是少不了的,当然啦,要和activity通信,回调也是少不了的,后面会详细介绍。不多说了,完整代码来一份:
MainActivity.Java
- package com.example.liang.dialogdemo;
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.support.v7.app.ActionBarActivity;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.Toast;
- public class MainActivity extends ActionBarActivity implements View.OnClickListener{
- private Button btn1;
- private Button btn2;
- private Button btn3;
- private Button btn4;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- btn1= (Button) findViewById(R.id.main_btn1);
- btn1.setOnClickListener(this);
- btn2= (Button) findViewById(R.id.main_btn2);
- btn2.setOnClickListener(this);
- btn3= (Button) findViewById(R.id.main_btn3);
- btn3.setOnClickListener(this);
- btn4= (Button) findViewById(R.id.main_btn4);
- btn4.setOnClickListener(this);
- }
- private void dialog1(){
- AlertDialog.Builder builder=new AlertDialog.Builder(this); //先得到构造器
- builder.setTitle("提示"); //设置标题
- builder.setMessage("是否确认退出?"); //设置内容
- builder.setIcon(R.mipmap.ic_launcher);//设置图标,图片id即可
- builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { //设置确定按钮
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss(); //关闭dialog
- Toast.makeText(MainActivity.this, "确认" + which, Toast.LENGTH_SHORT).show();
- }
- });
- builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { //设置取消按钮
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- Toast.makeText(MainActivity.this, "取消" + which, Toast.LENGTH_SHORT).show();
- }
- });
- builder.setNeutralButton("忽略", new DialogInterface.OnClickListener() {//设置忽略按钮
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- Toast.makeText(MainActivity.this, "忽略" + which, Toast.LENGTH_SHORT).show();
- }
- });
- //参数都设置完成了,创建并显示出来
- builder.create().show();
- }
- private void dialog1_1(){
- //先new出一个监听器,设置好监听
- DialogInterface.OnClickListener dialogOnclicListener=new DialogInterface.OnClickListener(){
- @Override
- public void onClick(DialogInterface dialog, int which) {
- switch(which){
- case Dialog.BUTTON_POSITIVE:
- Toast.makeText(MainActivity.this, "确认" + which, Toast.LENGTH_SHORT).show();
- break;
- case Dialog.BUTTON_NEGATIVE:
- Toast.makeText(MainActivity.this, "取消" + which, Toast.LENGTH_SHORT).show();
- break;
- case Dialog.BUTTON_NEUTRAL:
- Toast.makeText(MainActivity.this, "忽略" + which, Toast.LENGTH_SHORT).show();
- break;
- }
- }
- };
- //dialog参数设置
- AlertDialog.Builder builder=new AlertDialog.Builder(this); //先得到构造器
- builder.setTitle("提示"); //设置标题
- builder.setMessage("是否确认退出?"); //设置内容
- builder.setIcon(R.mipmap.ic_launcher);//设置图标,图片id即可
- builder.setPositiveButton("确认",dialogOnclicListener);
- builder.setNegativeButton("取消", dialogOnclicListener);
- builder.setNeutralButton("忽略", dialogOnclicListener);
- builder.create().show();
- }
- private void dialog2() {
- final String items[]={"张三","李四","王五"};
- //dialog参数设置
- AlertDialog.Builder builder=new AlertDialog.Builder(this); //先得到构造器
- builder.setTitle("提示"); //设置标题
- //builder.setMessage("是否确认退出?"); //设置内容
- builder.setIcon(R.mipmap.ic_launcher);//设置图标,图片id即可
- //设置列表显示,注意设置了列表显示就不要设置builder.setMessage()了,否则列表不起作用。
- builder.setItems(items,new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- Toast.makeText(MainActivity.this, items[which], Toast.LENGTH_SHORT).show();
- }
- });
- builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
- }
- });
- builder.create().show();
- }
- private void dialog3(){
- final String items[]={"男","女"};
- AlertDialog.Builder builder=new AlertDialog.Builder(this); //先得到构造器
- builder.setTitle("提示"); //设置标题
- builder.setIcon(R.mipmap.ic_launcher);//设置图标,图片id即可
- builder.setSingleChoiceItems(items,0,new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- //dialog.dismiss();
- Toast.makeText(MainActivity.this, items[which], Toast.LENGTH_SHORT).show();
- }
- });
- builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
- }
- });
- builder.create().show();
- }
- private void dialog4(){
- final String items[]={"篮球","足球","排球"};
- final boolean selected[]={true,false,true};
- AlertDialog.Builder builder=new AlertDialog.Builder(this); //先得到构造器
- builder.setTitle("提示"); //设置标题
- builder.setIcon(R.mipmap.ic_launcher);//设置图标,图片id即可
- builder.setMultiChoiceItems(items,selected,new DialogInterface.OnMultiChoiceClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which, boolean isChecked) {
- // dialog.dismiss();
- Toast.makeText(MainActivity.this, items[which]+isChecked, Toast.LENGTH_SHORT).show();
- }
- });
- builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
- //android会自动根据你选择的改变selected数组的值。
- for (int i=0;i<selected.length;i++){
- Log.e("hongliang",""+selected[i]);
- }
- }
- });
- builder.create().show();
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()){
- case R.id.main_btn1:
- // dialog1();
- dialog1_1();//等同与上面的dialog1(),比之更简洁,推荐
- break;
- case R.id.main_btn2:
- dialog2();
- break;
- case R.id.main_btn3:
- dialog3();
- break;
- case R.id.main_btn4:
- dialog4();
- break;
- }
- }
- }
- <LinearLayout 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:orientation="vertical"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".MainActivity">
- <Button
- android:id="@+id/main_btn1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="普通对话框" />
- <Button
- android:id="@+id/main_btn2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="列表对话框" />
- <Button
- android:id="@+id/main_btn3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="单选列表对话框" />
- <Button
- android:id="@+id/main_btn4"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="多选列表对话框" />
- </LinearLayout>
作者:laochidao 发表于2016/10/2 19:36:43 原文链接
阅读:87 评论:0 查看评论