今天无聊的时候学习了android中的Dialog的使用,在这里记录下Dialog的使用方法:
package com.ceo.activity;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
//这里记录了进度条Dialog,自定义 Dialog, AlertDialog的使用。
public class DialogTestActivity extends Activity {
static final int PROGRESS_DIALOG = 0;
Button progressButton = null;
Button customButton = null;
Button alertButton = null;
ProgressThread progressThread = null;
ProgressDialog progressDialog = null;
//申明一个handler用于处理progressDialog异步加载进度条
final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
int total = msg.arg1;
progressDialog.setProgress(total);
if(total>=100){
dismissDialog(PROGRESS_DIALOG);
progressThread.setmState(ProgressThread.STATE_NONE);
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//初始化buttons,并绑定监听事件
progressButton = (Button) this.findViewById(R.id.progressButton);
customButton = (Button) this.findViewById(R.id.customButton);
alertButton = (Button) this.findViewById(R.id.alertButton);
progressButton.setOnClickListener(new ButtonListener());
customButton.setOnClickListener(new ButtonListener());
alertButton.setOnClickListener(new ButtonListener());
}
//button的监听事件处理类
private class ButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
if (v == progressButton) {
//activity自带的显示dialog的方法
showDialog(PROGRESS_DIALOG);
}else if (v == customButton) {
Dialog dialog =new Dialog(DialogTestActivity .this);
//设置customDialog的View
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
dialog.show();
}else if (v == alertButton) {
//使用LayoutInflater将XML视图转换成View对象
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.custom_dialog, (ViewGroup)findViewById(R.id.layout_root));
//设置xml视图中imageView的imageResource
ImageView imageView = (ImageView)view.findViewById(R.id.image);
imageView.setImageResource(R.drawable.ic_launcher);
TextView textView = (TextView)view.findViewById(R.id.text);
textView.setText("Hello, this is a custom dialog!");
Builder builder = new AlertDialog.Builder(DialogTestActivity .this);
builder.setView(view);
builder.create().show();
}
}
}
/*Activity的用于创建dialog的方法,用这种方法创建的dialog,将与activity绑定在一起, 该方法只在第一次创建Dialog时调用,创建之后的dialog由activity管理
在这个方法中可用于dialog的创建。
**/
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case PROGRESS_DIALOG:
progressDialog = new ProgressDialog(DialogTestActivity .this);
progressDialog.setMessage("Loding....");
//设置Dialog的风格 : 水平
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
return progressDialog;
default:
return null;
}
}
//该方法在每次dialog被打开的时候,都会被调用。这里可以设置dialog的一些设置
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case PROGRESS_DIALOG:
progressDialog.setProgress(0);
progressThread = new ProgressThread(handler);
progressThread.start();
break;
default:
break;
}
}
//用于处理progressDialog的进度的走动。使用handler进行异步调用Thread,改变progressDialog的进度。
private class ProgressThread extends Thread{
Handler handler;
int mState = 0;
//Thread的停止状态
static final int STATE_NONE = 0;
//Thread的运行状态
static final int STATE_RUNNTIEM=1;
int total = 0;
public ProgressThread(Handler handler){
this.handler = handler;
}
@Override
public void run() {
mState = STATE_RUNNTIEM;
while (mState == STATE_RUNNTIEM) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//获得message对象,用sendMessage发送消息,由handler的handleMessage方法处理发送的消息
Message msg = handler.obtainMessage();
msg.arg1 = total;
handler.sendMessage(msg);
total++;
}
}
public void setmState(int mState) {
this.mState = mState;
}
}
}
这里要注意的是,在实例化Dialog的时候,传递给Dialog对象的context,不能用getApplicationContext()方法获得的context,不然会报异常:(代码中的红色代码)
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application,
导致报这个错是在于new AlertDialog.Builder(mcontext),虽然这里的参数是AlertDialog.Builder(Context context)但我们不能使用getApplicationContext()获得的Context,而必须使用Activity,因为只有一个Activity才能添加一个窗体。
解决方法:将new AlertDialog.Builder(Context context)中的参数用Activity.this(Activity是你的Activity的名称)来填充就可以正确的创建一个Dialog了。
开始屏幕:
ProgressDialog:
CustomDialog:
Alert Dialog:
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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/progressButton"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="@string/progressButton"/>
<Button
android:id="@+id/customButton"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="@string/customButton"/>
<Button
android:id="@+id/alertButton"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="@string/alertButton"/>
</LinearLayout>