在Activity中可以調用showDialog()來顯示一個對話框,覆蓋Activity的onCreateDialog方法,在這個方法中創建對話框,返回一個Dialog對象。
1.最簡單的對話框
[java]
AlertDialog.Builder b=new AlertDialog.Builder(this);
b.setTitle("簡單的");
b.setMessage("this is a simple dialog");
b.setPositiveButton("是", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
b.setNegativeButton("否", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
return b.create();
AlertDialog.Builder b=new AlertDialog.Builder(this);
b.setTitle("簡單的");
b.setMessage("this is a simple dialog");
b.setPositiveButton("是", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
b.setNegativeButton("否", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
return b.create();
效果如下
2.列表對話框
[java]
b.setTitle("列表");
//b.setMessage("message");這行代碼不要有
b.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(AndroidDialogActivity.this, items[which], Toast.LENGTH_SHORT).show();
}
});
return b.create();
b.setTitle("列表");
//b.setMessage("message");這行代碼不要有
b.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(AndroidDialogActivity.this, items[which], Toast.LENGTH_SHORT).show();
}
});
return b.create();items是一個String數組
效果圖
3.單選對話框
[java]
b.setTitle("請選擇顏色");
b.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(AndroidDialogActivity.this, items[which], Toast.LENGTH_SHORT).show();
}
});
b.setPositiveButton("是", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
b.setNegativeButton("否", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
return b.create();
b.setTitle("請選擇顏色");
b.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(AndroidDialogActivity.this, items[which], Toast.LENGTH_SHORT).show();
}
});
b.setPositiveButton("是", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
b.setNegativeButton("否", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
return b.create();
效果圖
4.多選對話框
[java]
boolean []ddd=new boolean[3];
b.setTitle("請選擇顏色");
b.setMultiChoiceItems(items, ddd, new DialogInterface.OnMultiChoiceClickListener(){
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
// TODO Auto-generated method stub
}
});
return b.create();
boolean []ddd=new boolean[3];
b.setTitle("請選擇顏色");
b.setMultiChoiceItems(items, ddd, new DialogInterface.OnMultiChoiceClickListener(){
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
// TODO Auto-generated method stub
}
});
return b.create();
效果圖
5.進度條對話框
[java]
Handler hand=new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
if(progressint>=100)
{
pd.dismiss();
}
else
{
progressint++;
pd.setProgress(progressint);
hand.sendEmptyMessageDelayed(0, 100);
}
}
};
pd=new ProgressDialog(this);
pd.setTitle("進度對話框");
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMax(100);
AndroidDialogActivity.this.hand.sendEmptyMessage(0);
pd.setButton(DialogInterface.BUTTON_POSITIVE, "確定", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}});
return pd;
Handler hand=new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
if(progressint>=100)
{
pd.dismiss();
}
else
{
progressint++;
pd.setProgress(progressint);
hand.sendEmptyMessageDelayed(0, 100);
}
}
};
pd=new ProgressDialog(this);
pd.setTitle("進度對話框");
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMax(100);
AndroidDialogActivity.this.hand.sendEmptyMessage(0);
pd.setButton(DialogInterface.BUTTON_POSITIVE, "確定", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}});
return pd;
效果圖
6.代碼自定義對話框
[html]
EditText et=new EditText(this);
et.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD|InputType.TYPE_CLASS_TEXT);
b.setTitle("請輸入密碼");
b.setView(et);
return b.create();
EditText et=new EditText(this);
et.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD|InputType.TYPE_CLASS_TEXT);
b.setTitle("請輸入密碼");
b.setView(et);
return b.create();
效果圖
7.XML文件自定義對話框
xml文件
[html]
<?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/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="用戶名" />
<EditText
android:id="@+id/EditText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView"
/>
<TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="密碼" />
<EditText
android:id="@+id/EdiText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:inputType="textPassword"
/>
<Button
android:id="@+id/buttonyes"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="確定" />
</LinearLayout>
<?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/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="用戶名" />
<EditText
android:id="@+id/EditText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView"
/>
<TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="密碼" />
<EditText
android:id="@+id/EdiText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:inputType="textPassword"
/>
<Button
android:id="@+id/buttonyes"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="確定" />
</LinearLayout>
Java代碼
[java]
b.setIcon(R.drawable.ic_launcher);
b.setTitle("自定義對話框");
LayoutInflater li=LayoutInflater.from(this);
View v=li.inflate(R.layout.info, null);
Button yes=(Button) v.findViewById(R.id.buttonyes);
yes.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(AndroidDialogActivity.this, "Hello World", Toast.LENGTH_SHORT).show();
}});
b.setView(v);
return b.create();
b.setIcon(R.drawable.ic_launcher);
b.setTitle("自定義對話框");
LayoutInflater li=LayoutInflater.from(this);
View v=li.inflate(R.layout.info, null);
Button yes=(Button) v.findViewById(R.id.buttonyes);
yes.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(AndroidDialogActivity.this, "Hello World", Toast.LENGTH_SHORT).show();
}});
b.setView(v);
return b.create();
效果圖