Android AlertDialog 详解 + 代码
的有关信息介绍如下:Android AlertDialog 详解 + 代码:
setTitle :为对话框设置标题
setIcon :为对话框设置图标
setMessage:为对话框设置内容
setView : 给对话框设置自定义样式
setItems :设置对话框要显示的一个list,一般用于显示几个命令时。
setMultiChoiceItems :用来设置对话框显示一系列的复选框。
setNeutralButton :
setPositiveButton :给对话框添加"Yes"按钮
setNegativeButton :对话框添加"No"按钮
create : 创建对话框
show :显示对话框
android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" >
package cn.liu.dialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class DialogActivity extends Activity {
static final int PROGRESS_DIALOG = 2;
ProgressThread progressThread;
ProgressDialog progressDialog;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button) this.findViewById(R.id.button1);
Button button2 = (Button) this.findViewById(R.id.button2);
Button button3 = (Button) this.findViewById(R.id.button3);
Button button4 = (Button) this.findViewById(R.id.button4);
Button button5 = (Button) this.findViewById(R.id.button5);
Button button6 = (Button) this.findViewById(R.id.button6);
// 给按钮1添加事件
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(
DialogActivity.this);
builder.setTitle("About me");
builder.setMessage("Westyi");
builder.setCancelable(true);
builder.create().show();
}
});
// 给按钮1添加事件
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(
DialogActivity.this);
builder.setMessage("是否退出应用?")// 提示消息
.setCancelable(true)// 将对话框设为不可取消(不能使用back键来取消)
.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
// 关闭应用
public void onClick(DialogInterface dialog,
int which) {
DialogActivity.this.finish();
}
}).setNegativeButton("NO",
new DialogInterface.OnClickListener() {
// 关闭对话框
public void onClick(DialogInterface dialog,
int which) {
dialog.cancel();
}
});
builder.create();// 创建
builder.show();// 初始化显示
}
});
// 给按钮3添加事件
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final CharSequence[] items = { "红色", "绿色", "蓝色", "白色" }; // 设置选择内容
AlertDialog.Builder builder = new AlertDialog.Builder(
DialogActivity.this);
builder.setTitle("选择你喜欢的颜色");// 设置标题
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item],
Toast.LENGTH_SHORT).show();
}
});
builder.create().show();
}
});
// 给按钮4添加事件
button4.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final CharSequence[] items = { "红色", "绿色", "蓝色", "白色" }; // 设置选择内容
AlertDialog.Builder builder = new AlertDialog.Builder(
DialogActivity.this);
builder.setTitle("选择你喜欢的颜色");// 设置标题
// 第二个参数是默认被选中的选项位置,使用“-1”来表示默认情况下不选中任何选项。
builder.setSingleChoiceItems(items, -1,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(),
items[item], Toast.LENGTH_SHORT).show();
// dialog.cancel();
}
});
builder.create().show();
}
});
// 给按钮5添加事件
button5.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final CharSequence[] items = { "红色", "绿色", "蓝色", "白色" }; // 设置选择内容
boolean[] checkedItems = { true, true, false, true };// 设置默认选中
AlertDialog.Builder builder = new AlertDialog.Builder(
DialogActivity.this);
builder.setTitle("选择你喜欢的颜色");// 设置标题
builder.setMultiChoiceItems(items, checkedItems,
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog,
int which, boolean isChecked) {
Toast.makeText(getApplicationContext(),
isChecked + "内容:" + items[which],
Toast.LENGTH_SHORT).show();
}
});
builder.create().show();
}
});
// 给按钮6添加事件
button6.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(PROGRESS_DIALOG);
}
});
}
protected Dialog onCreateDialog(int id) {
switch (id) {
case PROGRESS_DIALOG:
progressDialog = new ProgressDialog(DialogActivity.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("Loading...");
progressThread = new ProgressThread(handler);
progressThread.start();
return progressDialog;
default:
return null;
}
}
// Define the Handler that receives messages from the thread and update the
// progress
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
int total = msg.getData().getInt("total");
progressDialog.setProgress(total);
if (total >= 100) {
dismissDialog(PROGRESS_DIALOG);
progressThread.setState(ProgressThread.STATE_DONE);
}
}
};
/** Nested class that performs progress calculations (counting) */
private class ProgressThread extends Thread {
Handler mHandler;
final static int STATE_DONE = 0;
final static int STATE_RUNNING = 1;
int mState;
int total;
ProgressThread(Handler h) {
mHandler = h;
}
public void run() {
mState = STATE_RUNNING;
total = 0;
while (mState == STATE_RUNNING) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Log.e("ERROR", "Thread Interrupted");
}
Message msg = mHandler.obtainMessage();
Bundle b = new Bundle();
b.putInt("total", total);
msg.setData(b);
mHandler.sendMessage(msg);
total++;
}
}
/*
* sets the current state for the thread, used to stop the thread
*/
public void setState(int state) {
mState = state;
}
}
}