AlertDialog是一种在开发中普遍用到的控件。而且他的扩展性非常强,今天着重介绍下其用法...

![]()

![]()

![]()

![]()

![]()

![]()

![]()

![]()
-------
activity_main.xml
<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"
tools:context="com.example.activity.MainActivity" >
<Button
android:id="@+id/btn_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="带取消,中立,确定的弹出框" />
<Button
android:id="@+id/btn_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示带列表的弹出框" />
<Button
android:id="@+id/btn_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示带单选列表的弹出框" />
<Button
android:id="@+id/btn_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示带列表的弹出框_ArrayAdapter" />
<Button
android:id="@+id/btn_5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示带列表的弹出框_SimpleAdapter" />
<Button
android:id="@+id/btn_6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示带多选列表的弹出框" />
<Button
android:id="@+id/btn_7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示带自定义布局弹出框" />
<Button
android:id="@+id/btn_8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示带自定义带ListView布局弹出框" />
</LinearLayout>
-----custom_listview.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="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></ListView>
</LinearLayout>
-----custom_login.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="match_parent"
android:orientation="vertical" >
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名"
android:textColorHint="#ff0000" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:password="true"
android:textColorHint="#ff0000" />
</LinearLayout>
----list_item1.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="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal" >
<ImageView
android:id="@+id/iv_img"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/img1" />
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="美女1号"
android:textSize="20dp"/>
</LinearLayout>
----MainActivity.java
package com.example.activity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private Button btn_1;
private Button btn_2;
private Button btn_3;
private Button btn_4;
private Button btn_5;
private Button btn_6;
private Button btn_7;
private Button btn_8;
private List<Map<String, Object>> list;
// 定义数组
private String[] strs = { "美女1号", "美女2号", "美女3号", "美女4号", "美女5号", "美女6号", "美女7号", "美女8号", "美女9号", "美女10号", "美女11号",
"美女12号" };
private int[] images = { R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5,
R.drawable.img6, R.drawable.img7, R.drawable.img8, R.drawable.img9, R.drawable.img10, R.drawable.img11,
R.drawable.img12 };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
getData();
}
private void getData() {
list = new ArrayList<Map<String, Object>>();
for (int i = 0; i < strs.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", strs[i]);
map.put("img", images[i]);
list.add(map);
}
}
//初始化控件
private void initView() {
btn_1 = (Button) this.findViewById(R.id.btn_1);
btn_2 = (Button) this.findViewById(R.id.btn_2);
btn_3 = (Button) this.findViewById(R.id.btn_3);
btn_4 = (Button) this.findViewById(R.id.btn_4);
btn_5 = (Button) this.findViewById(R.id.btn_5);
btn_6 = (Button) this.findViewById(R.id.btn_6);
btn_7 = (Button) this.findViewById(R.id.btn_7);
btn_8 = (Button) this.findViewById(R.id.btn_8);
btn_1.setOnClickListener(this);
btn_2.setOnClickListener(this);
btn_3.setOnClickListener(this);
btn_4.setOnClickListener(this);
btn_5.setOnClickListener(this);
btn_6.setOnClickListener(this);
btn_7.setOnClickListener(this);
btn_8.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_1:
btn_1();
break;
case R.id.btn_2:
btn_2();
break;
case R.id.btn_3:
btn_3();
break;
case R.id.btn_4:
btn_4();
break;
case R.id.btn_5:
btn_5();
break;
case R.id.btn_6:
btn_6();
break;
case R.id.btn_7:
btn_7();
break;
case R.id.btn_8:
btn_8();
break;
}
}
/**
* 显示带自定义布局ListView弹出框
*/
private void btn_8() {
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("用户登陆");
//添加自定义布局文件
ListView listview = new ListView(this);
SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.list_item1, new String[] { "img", "name" },
new int[] { R.id.iv_img, R.id.tv_name });
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "点击了"+list.get(which).get("name").toString(), Toast.LENGTH_LONG).show();
}
});
builder.setView(listview);
builder.show();
}
/**
* 显示带自定义布局弹出框
*/
private void btn_7() {
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("用户登陆");
//添加自定义布局文件
View view=getLayoutInflater().inflate(R.layout.custom_login, null);
builder.setView(view);
//添加登陆和取消
builder.setPositiveButton("登陆", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "登陆成功", 1).show();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
/**
* 显示带多选列表的弹出框
*/
private void btn_6() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请选择");
final boolean[] checkedItems = new boolean[strs.length];
// 参数1:数组数据源 参数2:哪几项被选中 参数3事件
builder.setMultiChoiceItems(strs, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
//如果选择中了,要更新checkedItems的值
checkedItems[which]=isChecked;
}
});
//添加确定
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String mmmm="";
for (int i = 0; i < checkedItems.length; i++) {
if(checkedItems[i]){
mmmm+=strs[i];
}
}
Toast.makeText(getApplicationContext(), mmmm+"被选中", 1).show();
}
});
builder.show();
}
/**
* 显示带列表的弹出框_SimpleAdapter
*/
private void btn_5() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请选择");
// 创建适配器
SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.list_item1, new String[] { "img", "name" },
new int[] { R.id.iv_img, R.id.tv_name });
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), strs[which], 1).show();
}
});
// 显示出来
builder.show();
}
/**
* 显示带列表的弹出框_ArrayAdapter
*/
private void btn_4() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请选择");
// 创建适配器
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strs);
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), strs[which], 1).show();
}
});
builder.show();
}
/**
* 显示带单选列表的弹出框
*/
public void btn_3() {
AlertDialog.Builder builder3 = new AlertDialog.Builder(this);
builder3.setTitle("你要哪一个?");
// 定义一个数组去存放哪个一项目初选中
final boolean[] flag = new boolean[strs.length];
builder3.setSingleChoiceItems(strs, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
flag[which] = true;
// Toast.makeText(getApplicationContext(), strs[which],
// Toast.LENGTH_SHORT).show();
}
});
// 添加一个确定
builder3.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for (int i = 0; i < flag.length; i++) {
if (flag[i]) {
Toast.makeText(getApplicationContext(), strs[i], Toast.LENGTH_SHORT).show();
break;
}
}
}
});
builder3.show();
}
/**
* 显示带列表的弹出框
*/
public void btn_2() {
// 显示带列表的弹出框
AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
builder2.setTitle("你要哪一个");
// builder2.setItems(strs, new DialogInterface.OnClickListener() {
//
// @Override
// public void onClick(DialogInterface dialog, int which) {
// Toast.makeText(getApplicationContext(), strs[which],
// Toast.LENGTH_SHORT).show();
// }
// });
// 从资源文件里面取数据
final String[] items = this.getResources().getStringArray(R.array.arrays);
builder2.setItems(R.array.arrays, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), items[which], Toast.LENGTH_SHORT).show();
}
});
builder2.show();
}
/**
* 带取消,中立,确定的弹出框
*/
public void btn_1() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("我是标题");
builder.setMessage("我是内容");
builder.setIcon(R.drawable.ic_launcher);// 设置标题上的图片
// 添加确定Button
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "点击了确定", Toast.LENGTH_SHORT).show();
}
});
// 添加取消
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "点击了取消", Toast.LENGTH_SHORT).show();
}
});
// 添加中立
builder.setNeutralButton("中立", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "点击了中立", Toast.LENGTH_SHORT).show();
}
});
builder.show();
}
}
效果图如下
----
带取消,中立,确定的弹出框
----
显示带列表的弹出框
----
显示带单选列表的弹出框
----
显示带列表的弹出框_ArrayAdapter
----
显示带列表的弹出框_SimpleAdapter
----
显示带多选列表的弹出框
----
显示带自定义布局弹出框
----
显示带自定义布局ListView弹出框
转载请注明出处。。。