
使用大全
public class MainActivity extends ListActivity {
private List<String> mList;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
String[] array = { "普通Dialog", "確定取消對話框 AlertDialog", "單選對話框 AlertDialog", "多選對話框 AlertDialog", //
"自定義View的Dialog---注意:不同主題下顯示效果大不相同", "自定義View的AlertDialog---注意:不同主題下顯示效果大不相同", //
"自定義Dialog的位置", "自定義AlertDialog的位置", //
"進度條對話框 ProgressDialog", "帶進度的進度條對話框 ProgressDialog", };
for (int i = 0; i < array.length; i++) {
array[i] = i + "、" + array[i];
}
mList = new ArrayList<String>(Arrays.asList(array));
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mList));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
switch (position) {
case 0://普通Dialog
showDialog(this);
break;
case 1://確定取消對話框 AlertDialog
showSureOrCancleDialog(this);
break;
case 2://單選對話框 AlertDialog
showSingleChoiceDialog(this);
break;
case 3://多選對話框 AlertDialog
showMultiChoiceDialog(this);
break;
case 4://自定義View的Dialog---注意:不同主題下顯示效果大不相同
showCustomDialog(this);
break;
case 5://自定義View的AlertDialog---注意:不同主題下顯示效果大不相同
showCustomAlertDialog(this);
break;
case 6://自定義Dialog的位置
showDialogAnyWhere(this);
break;
case 7://自定義AlertDialog的位置
showAlertDialogAnyWhere(this);
break;
case 8://進度條對話框 ProgressDialog
showSimpleProgressDialog(this);
break;
case 9://帶進度的進度條對話框 ProgressDialog
showProgressDialog(this);
break;
}
}
//Dialog,基本上沒有什么API可用的***************************************************************************************************************
public static void showDialog(Context context) {
Dialog dialog = new Dialog(context);
dialog.setTitle("基本上沒有什么API可用的");
dialog.show();
}
/**確定取消對話框,旋轉屏幕后對話框消失*/
public static void showSureOrCancleDialog(final Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context).setTitle("標題").setMessage("內容")//
.setCancelable(false)//點擊返回鍵或對話框外部時是否消失,默認為true
.setNegativeButton("取消", null)//
.setPositiveButton("確定", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//參數:dialog-The dialog that received the click.;which-The button that was clicked
Toast.makeText(context, "確定被點擊了", Toast.LENGTH_SHORT).show();
}
});
builder.create().show();
}
/**單選對話框*/
public static void showSingleChoiceDialog(final Context context) {
final String[] items = { "男", "女", "未知" };
AlertDialog.Builder builder = new AlertDialog.Builder(context).setTitle("請選擇您的性別")//
.setSingleChoiceItems(items, -1, new OnClickListener() {//第二個參數指定選擇的是哪個,-1代表默認沒有選中
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(context, "您的性別:" + items[which], Toast.LENGTH_SHORT).show();
}
}).setPositiveButton("確定", null).setNegativeButton("取消", null);
builder.create().show();
}
/**多選對話框*/
public static void showMultiChoiceDialog(final Context context) {
final String[] items = { "蘋果", "梨", "菠蘿", "香蕉", "黃瓜" };
final boolean[] result = new boolean[] { true, false, true, false, false };//對應條目默認是否被選中
AlertDialog.Builder builder = new AlertDialog.Builder(context).setTitle("請選擇你最愛吃的水果")//
.setMultiChoiceItems(items, result, new OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
String inf = "";
if (isChecked) {
inf = " 被選中了";
} else {
inf = " 被取消選中了";
}
Toast.makeText(context, items[which] + inf, Toast.LENGTH_SHORT).show();
result[which] = isChecked;
}
})//
.setPositiveButton("提交", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < result.length; i++) {
if (result[i]) {
sb.append(items[i] + " ");
}
}
Toast.makeText(context, "您選中了:" + sb.toString(), Toast.LENGTH_SHORT).show();
}
});
builder.create().show();
}
//自定義View的Dialog。注意:不同主題下顯示效果大不相同*********************************************************************************************
public static void showCustomDialog(Context context) {
Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.layout_dialog);
dialog.show();
}
public static void showCustomAlertDialog(Context context) {
new AlertDialog.Builder(context).setView(R.layout.layout_dialog).create().show();
}
//自定義對話框顯示位置**********************************************************************************************************************************
public static void showDialogAnyWhere(Context context) {
Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.layout_dialog2);
Window dialogWindow = dialog.getWindow(); // 獲取對話框窗口對象
dialogWindow.setBackgroundDrawable(new ColorDrawable(0xffff0000));
dialogWindow.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);//修改對話框的布局設置,和mLayoutParams.gravity屬性的值一致
WindowManager.LayoutParams mLayoutParams = dialogWindow.getAttributes();//獲取對話框參數對象
mLayoutParams.x = 0;// 表示相對於【原始位置】的偏移,當為Gravity.LEFT時就表示相對左邊界的偏移,正值右移,負值忽略
mLayoutParams.y = 20;//當為Gravity.BOTTOM時正值上移,負值忽略
mLayoutParams.height = 900;//寬高設置是否有效也是和主題有關的
mLayoutParams.alpha = 0.8f; // 透明度
dialogWindow.setAttributes(mLayoutParams);
dialog.show();
}
public static void showAlertDialogAnyWhere(Context context) {
AlertDialog dialog = new AlertDialog.Builder(context).setView(R.layout.layout_dialog2).create();
Window dialogWindow = dialog.getWindow(); // 獲取對話框窗口對象
dialogWindow.setBackgroundDrawable(new ColorDrawable(0xffff0000));
dialogWindow.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);//修改對話框的布局設置,和mLayoutParams.gravity屬性的值一致
WindowManager.LayoutParams mLayoutParams = dialogWindow.getAttributes();//獲取對話框參數對象
mLayoutParams.x = 0;// 表示相對於【原始位置】的偏移,當為Gravity.LEFT時就表示相對左邊界的偏移,正值右移,負值忽略
mLayoutParams.y = 20;//當為Gravity.BOTTOM時正值上移,負值忽略
mLayoutParams.height = 900;//寬高設置是否有效也是和主題有關的
mLayoutParams.alpha = 0.8f; // 透明度
dialogWindow.setAttributes(mLayoutParams);
dialog.show();
}
//進度條對話框ProgressDialog********************************************************************************************************************
public static void showSimpleProgressDialog(Context context) {
final ProgressDialog pd = new ProgressDialog(context);//不需要創建器
pd.setTitle("提醒");
pd.setMessage("正在加載數據,請稍等...");
pd.setCancelable(false);
pd.show();
new Thread() {
public void run() {
SystemClock.sleep(2000);
pd.dismiss();//可以在子進程中關閉進度條。
//旋轉屏幕后ProgressDialog被銷毀了,而此線程會繼續運行,當調用pd.dismiss()時由於pd為空,所以會導致異常。Activity直接掛掉!
};
}.start();
}
/**帶進度的進度條對話框*/
public static void showProgressDialog(final Context context) {
final ProgressDialog pd = new ProgressDialog(context);
pd.setTitle("提醒");
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//當設置此屬性的值后才會有進度顯示
pd.setMax(100);
pd.setMessage("正在加載數據,請稍等...");
pd.setCancelable(false);
pd.show();
new Thread() {
public void run() {
for (int i = 0; i < 100; i++) {
SystemClock.sleep(20);
pd.setProgress(i);//改變當前進度。可以在子線程中改變進度條的進度。
}
pd.dismiss();//當進度為100%時關閉對話框
//僅為了演示Toast才這么搞!
Looper.prepare();
Toast.makeText(context, "加載完畢", Toast.LENGTH_SHORT).show();
Looper.loop();
};
}.start();
}
}
自定義Dialog的布局1
<?xml version="1.0" encoding="utf-8"?>
<!-- 注意:不同主題下顯示效果大不相同 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="180dp"
android:layout_height="400dp"
android:background="@android:color/holo_blue_light"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:inputType="textPassword" />
</LinearLayout>
自定義Dialog的布局2
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_light"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:inputType="textPassword" />
</LinearLayout>
Dialog相關API
public
class
Dialog
implements
DialogInterface, Window.Callback, KeyEvent.Callback, View.OnCreateContextMenuListener



AlertDialog是Dialog的一個直接子類
public class AlertDialog extends Dialog implements android.content.DialogInterface

一個AlertDialog可以有兩個以上的Button,可以對一個AlertDialog設置相應的信息,比如title、massage等等。
不能直接通過AlertDialog的構造函數來生產一個AlertDialog(AlertDialog所有的構造方法都是
protected),
只能通過
AlertDialog的靜態內部類Builder
來創建。

相比
Dialog(基本沒有什么set方法)
,AlertDialog使用起來相當方便,幾行代碼就可以搞定。
附件列表