Dialog/AlertDialog/ProgressDialog關(guān)系類圖
Dialog
1. 圓形旋轉(zhuǎn)進(jìn)度樣式
public Dialog createSpinnerDialog(Context context, String msg) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.spinner_style_dialog, null);
LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.ll_spinner_dialog);
ImageView imageView = (ImageView) view.findViewById(R.id.iv_spinner);
TextView textView = (TextView) view.findViewById(R.id.tv_spinner);
Animation animation = AnimationUtils.loadAnimation(context, R.anim.loading_animation);
imageView.startAnimation(animation);
textView.setText(msg);
Dialog dialog = new Dialog(context, R.style.loading_dialog);
dialog.setCancelable(true); // 設(shè)置按返回鍵時取消對話框
dialog.setCanceledOnTouchOutside(false); // 設(shè)置點擊對話框外部時不取消對話框,默認(rèn)為true
dialog.setContentView(linearLayout, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
));
return dialog;
}
創(chuàng)建對話框并顯示
createSpinnerDialog(this, "正在加載中...").show();
效果演示
2. 幀動畫進(jìn)度樣式
public Dialog createFrameDialog(Context context, String msg){
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.frame_style_dialog, null);
LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.ll_frame_dialog);
ImageView imageView = (ImageView) view.findViewById(R.id.iv_frame);
TextView textView = (TextView) view.findViewById(R.id.tv_frame);
AnimationDrawable ad = (AnimationDrawable) imageView.getDrawable();
ad.start();
textView.setText(msg);
Dialog dialog = new Dialog(context, R.style.loading_dialog); //
dialog.setCancelable(true); // 設(shè)置按返回鍵時取消對話框
dialog.setCanceledOnTouchOutside(false); // 設(shè)置點擊對話框外部時不取消對話框涌韩,默認(rèn)為true
dialog.setContentView(linearLayout, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
));
return dialog;
}
創(chuàng)建對話框并顯示
createFrameDialog(this, "正在加載中...").show();
效果演示
AlertDialog
1. 提示對話框
private void showAlertDialog() {
// 使用建造者模式創(chuàng)建對話框
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_android_green_a700_24dp); // 圖標(biāo)
builder.setTitle(R.string.text_alert_title); // 標(biāo)題
builder.setMessage(R.string.text_alert_msg); // 消息內(nèi)容
// 確定性質(zhì)的按鈕 (是项栏,有吼驶,確定)
builder.setPositiveButton(R.string.text_alert_btn_positive, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, R.string.text_alert_btn_positive_tip, Toast.LENGTH_SHORT).show();
}
});
// 否定性質(zhì)的按鈕 (否驶沼,沒有,取消)
builder.setNegativeButton(R.string.text_alert_btn_negative, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, R.string.text_alert_btn_negative_tip, Toast.LENGTH_SHORT).show();
}
});
// 中立性質(zhì)的按鈕 (不確定,沉默,保密假哎,忽略,詳細(xì))
builder.setNeutralButton(R.string.text_alert_btn_neutral, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, R.string.text_alert_btn_neutral_tip, Toast.LENGTH_SHORT).show();
}
});
// 通過建造者來創(chuàng)建對話框
AlertDialog dialog = builder.create(); // -
// 顯示對話框
dialog.show(); // -
// 直接使用建造者顯示對話框
// builder.show(); // +
}
效果演示
2. 簡單列表對話框
private void showSimpleListDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// builder.setTitle(R.string.text_list_simple_title);
final String[] platforms = getResources().getStringArray(R.array.platforms);
// 設(shè)置簡單列表的子項內(nèi)容
builder.setItems(platforms, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "Selected: " + platforms[which], Toast.LENGTH_SHORT).show();
}
});
builder.show();
}
效果演示
3. 單選列表對話框
private void showRadioListDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.text_list_radio_title);
final String[] cities = getResources().getStringArray(R.array.cities);
int checkedItem = 0;
final String[] result = {cities[checkedItem]};
// 設(shè)置單選子項的內(nèi)容
builder.setSingleChoiceItems(cities, checkedItem, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
result[0] = cities[which];
}
});
builder.setPositiveButton(R.string.text_list_radio_btn_positive, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "Selected: " + result[0], Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton(R.string.text_list_radio_btn_negative, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "Default: " + result[0], Toast.LENGTH_SHORT).show();
}
});
builder.show();
}
效果演示
4. 多選列表對話框
private void showCheckListDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.text_list_check_title);
final String[] languages = getResources().getStringArray(R.array.languages);
final List<String> results = new ArrayList<>();
// 設(shè)置多選子項內(nèi)容
builder.setMultiChoiceItems(languages, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
results.add(languages[which]);
} else {
results.remove(languages[which]);
}
}
});
builder.setPositiveButton(R.string.text_list_check_btn_positive, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, results.toString(), Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton(R.string.text_list_check_btn_negative, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, R.string.text_list_check_btn_negative, Toast.LENGTH_SHORT).show();
results.clear();
}
});
builder.show();
}
效果演示
5. 自定義適配器對話框
private void showCustomAdapterDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final List<User> mUserList = new ArrayList<>();
mUserList.add(new User(R.drawable.ic_user_avatar, "Stephen Curry"));
mUserList.add(new User(R.drawable.ic_user_avatar, "Kevin Durant"));
CustomAdapter adapter = new CustomAdapter(this, mUserList);
// 設(shè)置自定義數(shù)據(jù)適配器鞍历,用于內(nèi)部的ListView
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "Selected: " + mUserList.get(which).getName(), Toast.LENGTH_SHORT).show();
}
});
builder.show();
}
效果演示
6. 自定義視圖對話框
private void showCustomViewDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_user_signin);
builder.setTitle(R.string.text_custom_view_title);
// builder.setView(R.layout.layout_signin); // API21
final View view = getLayoutInflater().inflate(R.layout.layout_signin, null);
builder.setView(view); // 設(shè)置自定義內(nèi)容視圖
builder.setPositiveButton(R.string.text_custom_view_btn_positive, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText mUsernameEt = (EditText) view.findViewById(R.id.et_username);
EditText mPasswordEt = (EditText) view.findViewById(R.id.et_password);
String username = mUsernameEt.getText().toString();
String password = mPasswordEt.getText().toString();
String info = String.format(Locale.getDefault(), "[%s:%s]", username, password);
Toast.makeText(MainActivity.this, info, Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton(R.string.text_custom_view_btn_negative, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, R.string.text_custom_view_btn_negative, Toast.LENGTH_SHORT).show();
}
});
builder.show();
}
效果演示
ProgressDialog
ProgressDialog基本使用說明
1. 創(chuàng)建ProgressDialog的兩種方式
- 使用new來創(chuàng)建一個ProgressDialog實例
ProgressDialog dialog = new ProgressDialog(this);
dialog.show();
- 調(diào)用ProgressDialog中的靜態(tài)方法show()
ProgressDialog dialog = ProgressDialog.show(this, null, "正在加載...");
靜態(tài)方法show()的參數(shù)說明
public static ProgressDialog show(
Context context, // 上下文
CharSequence title, // 標(biāo)題
CharSequence message, // 消息內(nèi)容
boolean indeterminate, // 不確定性位谋,false為不確定性樣式,true為確定性樣式
boolean cancelable, // 是否可以取消
OnCancelListener cancelListener // 取消事件監(jiān)聽器
);
2. 取消ProgressDialog對話框
Dialog.cancel()
和Dialog.dismiss()
都可以刪除對話框堰燎,但是使用cancel()方法會在刪除對話框時回調(diào)DialogInterface.OnCancelListener
監(jiān)聽器中的onCancel()
方法掏父,而dismiss()則不會進(jìn)行回調(diào)。
3. 設(shè)置ProgressDialog進(jìn)度條樣式
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); // 設(shè)置水平進(jìn)度條
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); // 設(shè)置圓形旋轉(zhuǎn)進(jìn)度條
ProgressDialog使用示例
1. 圓形旋轉(zhuǎn)進(jìn)度條
private void createIndeterminateProgressDialog() {
final ProgressDialog dialog = ProgressDialog.show(this, null, "正在加載...");
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(3000);
dialog.dismiss();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
效果演示
2. 水平進(jìn)度條
private void createDeterminateProgressDialog() {
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setIcon(R.drawable.ic_action_download); // 設(shè)置圖標(biāo)
dialog.setTitle("當(dāng)前下載進(jìn)度"); // 設(shè)置標(biāo)題
dialog.setMessage("正在加載中..."); // 設(shè)置消息內(nèi)容
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); // 設(shè)置水平進(jìn)度條
dialog.setMax(100); // 設(shè)置最大進(jìn)度值
dialog.setCancelable(true); // 設(shè)置是否可以通過點擊Back鍵取消
dialog.setCanceledOnTouchOutside(false); // 設(shè)置在點擊Dialog外是否取消Dialog進(jìn)度條
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "確定", Toast.LENGTH_SHORT).show();
}
});
dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "忽略", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "忽略", Toast.LENGTH_SHORT).show();
}
});
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
}
});
// 按Back鍵事件監(jiān)聽
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
Toast.makeText(MainActivity.this, "Cancel", Toast.LENGTH_SHORT).show();
}
});
dialog.show();
new Thread(new Runnable() {
@Override
public void run() {
int i = 0;
while (i < 100) {
try {
i += 5;
Thread.sleep(200); // 5000ms
dialog.incrementProgressBy(5); // 更新進(jìn)度
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
dialog.dismiss();
}
}).start();
}
效果演示