1.介绍
2.作用
(1)消息提示对话框
(2)简单列表对话框
(3)单选列表对话框
(4)多选对话框
(5)自定义对话框
3.java后台代码
package com.lucky.test28dialog; import android.content.DialogInterface; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.SimpleAdapter; import android.widget.Toast; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MainActivity extends AppCompatActivity { Button button1; Button button2; Button button3; Button button4; Button button5; String[] strings={"大数据","物联网","云计算","无人机","机器人"}; boolean[] flags={false,false,false,false,false}; String userchoice; String temp=""; SimpleAdapter simpleAdapter; int[] images={R.mipmap.img01,R.mipmap.img02,R.mipmap.img03,R.mipmap.img04,R.mipmap.img05}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1=findViewById(R.id.button); button2=findViewById(R.id.button2); button3=findViewById(R.id.button3); button4=findViewById(R.id.button4); button5=findViewById(R.id.button5); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //实例化消息提示对话框 AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this); builder.setIcon(R.mipmap.ic_launcher); //设置图标 builder.setTitle("提示框"); //设置标题 builder.setMessage("我是消息提示对话框"); //设置提示内容 builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"您点击了确定",Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"您点击了取消",Toast.LENGTH_SHORT).show(); } }); builder.show(); //显示对话框 } }); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //实例化简单列表对话框 AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this); builder.setIcon(R.mipmap.ic_launcher); //设置图标 builder.setTitle("提示框"); //设置标题 //设置简单列表对话框里显示的内容 builder.setItems(strings, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,strings[which],Toast.LENGTH_SHORT).show(); } }); builder.show();//显示对话框 } }); button3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //实例化单选列表对话框 AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this); builder.setIcon(R.mipmap.ic_launcher); //设置图标 builder.setTitle("提示框"); //设置标题 //设置对话框的内容和监听器 builder.setSingleChoiceItems(strings, 1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { userchoice=strings[which]; } }); //设置对话框的确定和取消按钮 builder.setPositiveButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"您点击了取消",Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,userchoice,Toast.LENGTH_SHORT).show(); } }); builder.show();//显示对话框(天坑!这句一定不能忘) } }); button4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //实例化多选列表对话框 AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this); builder.setIcon(R.mipmap.ic_launcher); //设置图标 builder.setTitle("提示框"); //设置标题 //设置多选列表对话框的显示内容(strings),flags表示哪几项被选中 builder.setMultiChoiceItems(strings, flags, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { flags[which]=isChecked; //修改flags列表里的值 } }); builder.setPositiveButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"您点击了取消",Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { for (int i = 0; i <flags.length ; i++) { if(flags[i]){ temp+=strings[i]+";"; } } if(temp.equals("")){ Toast.makeText(MainActivity.this,"您未选择任何内容",Toast.LENGTH_SHORT).show(); return; //天秀:return可以提前结束方法的执行,若没有return,temp=temp.substring(0,temp.length()-1)会出错 } temp=temp.substring(0,temp.length()-1); Toast.makeText(MainActivity.this,temp,Toast.LENGTH_SHORT).show(); temp=""; } }); builder.show(); } }); button5.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //实例化自定义对话框 AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this); builder.setIcon(R.mipmap.ic_launcher); //设置图标 builder.setTitle("提示框"); //设置标题 List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); for (int i = 0; i < strings.length; i++) { Map<String, Object> map = new HashMap<String, Object>(); map.put("name", strings[i]); map.put("picture", images[i]); list.add(map); } simpleAdapter = new SimpleAdapter(MainActivity.this, list, R.layout.item, new String[] { "name", "picture" }, new int[] { R.id.textView1, R.id.imageView1 }); builder.setAdapter(simpleAdapter, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "您选择的是"+strings[arg1], Toast.LENGTH_SHORT).show(); } }); builder.show(); } }); } }
4.效果图