使用AlertDialog创建对话框
AlertDialog的功能非常强大,它不仅可以生产带按钮的提示对话框,还可以生成带列表的列表对话框,概况起来有一下4种:
1.带确定,中立,取消等N个按钮的提示对话框,按钮个数可以根据需要进行改变。
2.带列表的列表对话框
3.带多个单选列表项和N个按钮的列表对话框;
4.带多个多选列表项和N个按钮的列表对话框;
示例如下:
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"
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/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="带3个按钮的对话框"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="带列表的对话框"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="带单选列表项的对话框"/>
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="带3多选列表项的对话框"/>
</LinearLayout>
MainActivity.java代码如下:
package com.example.alertdialogtest;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button button1=null;
private Button button2=null;
private Button button3=null;
private Button button4=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog alert=new AlertDialog.Builder(MainActivity.this).create();
alert.setIcon(R.drawable.ic_launcher);
alert.setTitle("系统提示:");
alert.setMessage("带三个按钮的对话框");
//设置取消按钮
alert.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "您点击了取消按钮", Toast.LENGTH_SHORT).show();
}
});
//设置确定按钮
alert.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "您点击了确定按钮", Toast.LENGTH_SHORT).show();
}
});
//设置中立按钮
alert.setButton(DialogInterface.BUTTON_NEUTRAL, "中立", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "您点击了中立按钮", Toast.LENGTH_SHORT).show();
}
});
alert.show();
}
});
//为第二个按钮添加监听器
button2=(Button)findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final String[]items=new String[]{"跑步","羽毛球","乒乓球","网球","体操"};
Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("请选择你喜欢的运动项目");
//添加列表项
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "您选择了"+items[which], Toast.LENGTH_SHORT).show();
}
});
builder.create().show(); //显示对话框
}
});
button3=(Button)findViewById(R.id.button3);
button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final String[]itmes=new String[]{"标准","无声","会议","户外","离线"};
Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("请选择情景模式:");
builder.setSingleChoiceItems(itmes, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "您选择了"+itmes[which], Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("确定", null); //添加确定按钮
builder.create().show();
}
});
button4=(Button)findViewById(R.id.button4);
button4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final boolean []checkedItems=new boolean[]{false,true,false,true,false};
final String []items=new String[]{"植物大战僵尸","愤怒的小鸟","泡泡龙","开心农场","超级玛丽"};
Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("请选择您喜爱的游戏");
builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// TODO Auto-generated method stub
checkedItems[which]=isChecked;
}
});
//为对话框添加确定按钮
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
String result=""; //用于保存选择结果
for(int i=0;i<checkedItems.length;i++){
if(checkedItems[i]){
result+=items[i]+"、";
}
}
if(!"".equals(result)){
result=result.substring(0, result.length()-1);
Toast.makeText(MainActivity.this, "您选择了["+result+"]", Toast.LENGTH_SHORT).show();
}
}
});
builder.create().show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}