Android中要显现对话框需使用AlterDialog.Builder类,还可以自定义对话框。对话框如果存在按钮,还需要对其设置事件监听OnClickListener。
使用AlterDialog.Builder传几个键对话框用到的几个方法:
setTitle() 设置对话框标题
setIcon()设置对话框图标
setMessage()设置对话框提示信息
setItems()设置对话框要显示的一个list,一般用于显示几个命令时
setSingleChoiceItems()设置对话框显示一个单选的list
setMultiChoiceItems()设置对话框显示一系列的复选框
setPositiveButton()添加yes按钮
setNegativeButton()添加NO按钮
下面的的例子分别建立了一个打两个按钮的对话框和一个带进度条的对话框!

myDialogActivity.java
package cn.myDialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.TextView;
public class myDialogActivity extends Activity {
ProgressDialog mDialog;
ImageView imageView1,imageView2;
TextView textView;
int imag_alpha=255;
boolean isrung=false;
Handler mHandler=new Handler();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView1=(ImageView) findViewById(R.id.image1);
imageView2=(ImageView) findViewById(R.id.image2);
textView=(TextView) findViewById(R.id.textview1);
// textView.setText("现在alpha的值是:"+Integer.toString(imag_alpha));
Dialog dialog=new AlertDialog.Builder(this).setTitle(" 登录提示")
.setMessage("这里需要登录")
.setPositiveButton("确定", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
isrung=true;
imageView1.setImageResource(R.drawable.wee);
imageView2.setImageResource(R.drawable.dd);
imageView1.setAlpha(imag_alpha);
imageView2.setAlpha(imag_alpha);
new Thread(new Runnable() {
public void run() {
while (isrung) {
try {
Thread.sleep(200);
updateAlpha();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
mHandler=new Handler(){
public void handleMessage(Message msg) {
super.handleMessage(msg);
imageView1.setAlpha(imag_alpha);
textView.setText("现在alpha的值是:"+Integer.toString(imag_alpha));
imageView1.invalidate();
}
};
}
}).setNegativeButton("取消", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
myDialogActivity.this.finish();
}
}).setIcon(R.drawable.dd).show();
mDialog=ProgressDialog.show(myDialogActivity.this, "请稍等", "正在为你处理...",true);
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(3000);
} catch (Exception e) {
// TODO: handle exception
}
finally
{mDialog.dismiss();}
}
}).start();
}
public void updateAlpha(){
if (imag_alpha-7>=0) {
imag_alpha-=7;
}
else{
imag_alpha=0;
isrung=false;
}
mHandler.sendMessage(mHandler.obtainMessage());
}
}