Android应用开发学习之AlertDialog对话框

作者:刘昊昱

博客:http://blog.csdn.net/liuhaoyutz

本文中我们通过一个例子来看AlertDialog对话框的实现,其运行效果如下:

Android应用开发学习之AlertDialog对话框

主布局文件main.xml内容如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="对话框应用示例:" />" <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮对话框" /> <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="多选列表对话框" /> </LinearLayout>

布局文件中只是简单的放置了4个按钮。

下面看主Activity文件,其内容如下:

package com.liuhaoyu;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast; public class MainActivity extends Activity {
private boolean[] checkedItems; /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
alert.setIcon(R.drawable.image01);
alert.setTitle("按钮对话框:");
alert.setMessage("带按钮的对话框!");
alert.setButton(DialogInterface.BUTTON_NEGATIVE,"取消", new OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "您单击了取消按钮",
Toast.LENGTH_SHORT).show(); }
});
alert.setButton(DialogInterface.BUTTON_POSITIVE,"确定", new OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "您单击了确定按钮",
Toast.LENGTH_SHORT).show(); }
});
alert.setButton(DialogInterface.BUTTON_NEUTRAL,"中立",new OnClickListener(){ @Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "您单击了中立按钮",
Toast.LENGTH_SHORT).show();
}
});
alert.show();
}
}); Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
final String[] items = new String[] { "苹果", "三星", "HTC", "联想", "小米" };
Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.image01);
builder.setTitle("请选择手机品牌:");
builder.setItems(items, new OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,
"您选择了" + items[which], Toast.LENGTH_SHORT).show(); }
});
builder.create().show();
}
}); Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
final String[] items = new String[] { "苹果", "三星", "HTC", "联想", "小米" };
Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.image01);
builder.setTitle("请选择手机品牌:");
builder.setSingleChoiceItems(items, 0, new OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,
"您选择了" + items[which], Toast.LENGTH_SHORT).show(); }
}); builder.setPositiveButton("确定", null);
builder.create().show();
}
}); Button button4 = (Button) findViewById(R.id.button4);
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkedItems= new boolean[] { false, true, false,true, false };
final String[] items = new String[] { "苹果", "三星", "HTC", "联想", "小米" };
Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.image01);
builder.setTitle("请选择手机品牌:");
builder.setMultiChoiceItems(items, checkedItems,
new OnMultiChoiceClickListener() { @Override
public void onClick(DialogInterface dialog,
int which, boolean isChecked) {
checkedItems[which]=isChecked;
}
});
builder.setPositiveButton("确定", new OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
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_LONG).show();
}
}
});
builder.create().show();
}
});
}
}

程序中为4个按钮设置了点击监听器,在监听器中创建了4种不同的对话框。

上一篇:网络地址转换(NAT)的报文跟踪


下一篇:华为防火墙接入专线nat转发问题