一、系統(tǒng)自帶dialog
AlertDialog
1赶撰、普通dialogsetMessage
/* @setIcon 設(shè)置對話框圖標(biāo)
* @setTitle 設(shè)置對話框標(biāo)題
* @setMessage 設(shè)置對話框消息提示
* setXXX方法返回Dialog對象舌镶,因此可以鏈?zhǔn)皆O(shè)置屬性
* */
@OnClick(R.id.dialog_1)
public void Dialog1(){
AlertDialog.Builder normalDialog = new AlertDialog.Builder(MainDialogActivity.this);
normalDialog.setIcon(R.mipmap.ic_launcher);
normalDialog.setTitle("我是一個普通Dialog");
normalDialog.setMessage("你要點(diǎn)擊哪一個按鈕呢?");
normalDialog.setPositiveButton("確定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showToastShort("點(diǎn)擊了確定");
}
});
normalDialog.setNegativeButton("關(guān)閉",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showToastShort("點(diǎn)擊了關(guān)閉");
}
});
// 顯示
normalDialog.show();
}
2、列表dialogsetitems
/*列表Dialog*/
@OnClick(R.id.dialog_2)
public void Dialog2(){
final String[] items = { "我是1","我是2","我是3","我是4" };
AlertDialog.Builder listDialog =
new AlertDialog.Builder(MainDialogActivity.this);
listDialog.setTitle("我是一個列表Dialog");
listDialog.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showToastShort("你點(diǎn)擊了" + items[which]);
}
});
listDialog.show();
}
3豪娜、單選dialogsetSingleChoiceItems
/*單選Dialog*/
@OnClick(R.id.dialog_3)
public void Dialog3(){
final String[] items = { "我是1","我是2","我是3","我是4" };
yourChoice = -1;
AlertDialog.Builder singleChoiceDialog =
new AlertDialog.Builder(MainDialogActivity.this);
singleChoiceDialog.setTitle("我是一個單選Dialog");
// 第二個參數(shù)是默認(rèn)選項(xiàng)餐胀,此處設(shè)置為0
singleChoiceDialog.setSingleChoiceItems(items, 0,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
yourChoice = which;
}
});
singleChoiceDialog.setPositiveButton("確定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (yourChoice != -1) {
showToastShort("你選擇了" + items[yourChoice]);
}
}
});
singleChoiceDialog.show();
}
4、多選dialogsetMultiChoiceItems
/*多選Dialog*/
@OnClick(R.id.dialog_4)
public void Dialog4(){
final String[] items = { "我是1","我是2","我是3","我是4" };
// 設(shè)置默認(rèn)選中的選項(xiàng)瘤载,全為false默認(rèn)均未選中
final boolean initChoiceSets[]={false,false,false,false};
yourChoices.clear();
AlertDialog.Builder multiChoiceDialog =
new AlertDialog.Builder(MainDialogActivity.this);
multiChoiceDialog.setTitle("我是一個多選Dialog");
multiChoiceDialog.setMultiChoiceItems(items, initChoiceSets,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
if (isChecked) {
yourChoices.add(which);
} else {
yourChoices.remove(which);
}
}
});
multiChoiceDialog.setPositiveButton("確定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int size = yourChoices.size();
String str = "";
for (int i = 0; i < size; i++) {
str += items[yourChoices.get(i)] + " ";
}
showToastShort("你選中了" + str);
}
});
multiChoiceDialog.show();
}
5否灾、編輯dialogsetView(editText)
@OnClick(R.id.dialog_7)
public void Dialog7(){
final EditText editText = new EditText(MainDialogActivity.this);
AlertDialog.Builder inputDialog =new AlertDialog.Builder(MainDialogActivity.this);
inputDialog.setTitle("我是一個輸入Dialog").setView(editText);
inputDialog.setPositiveButton("確定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showToastShort(editText.getText().toString());
}
}).show();
}
ProgressDialog
6、等待dialogsetIndeterminate
/*等待Dialog*/
@OnClick(R.id.dialog_5)
public void Dialog5(){
ProgressDialog waitingDialog= new ProgressDialog(MainDialogActivity.this);
waitingDialog.setTitle("我是一個等待Dialog");
waitingDialog.setMessage("等待中...");
waitingDialog.setIndeterminate(true);
waitingDialog.setCancelable(false);
waitingDialog.show();
}
7鸣奔、進(jìn)度條dialogsetProgressStyle
/*進(jìn)度條dailog
@setProgress 設(shè)置初始進(jìn)度
* @setProgressStyle 設(shè)置樣式(水平進(jìn)度條)
* @setMax 設(shè)置進(jìn)度最大值
*/
@OnClick(R.id.dialog_6)
public void Dialog6(){
final int MAX_PROGRESS = 100;
final ProgressDialog progressDialog =
new ProgressDialog(MainDialogActivity.this);
progressDialog.setProgress(0);
progressDialog.setTitle("我是一個進(jìn)度條Dialog");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMax(MAX_PROGRESS);
progressDialog.show();
/* 模擬進(jìn)度增加的過程墨技,新開一個線程,每個100ms挎狸,進(jìn)度增加1*/
new Thread(new Runnable() {
@Override
public void run() {
int progress= 0;
while (progress < MAX_PROGRESS){
try {
Thread.sleep(100);
progress++;
progressDialog.setProgress(progress);
} catch (InterruptedException e){
e.printStackTrace();
}
}
// 進(jìn)度達(dá)到最大值后扣汪,窗口消失
progressDialog.cancel();
}
}).start();
}
二、自定義dialog
有點(diǎn)類似于給Activity布局
1锨匆、自定義Dialog的xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="你看看天空"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
2崭别、自定義Dialog的Style
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="edit_dialog" parent="Theme.AppCompat.Dialog">
<item name="windowNoTitle" />
</style>
</resources>
- dialog的Style的
其他屬性
<!--最近做項(xiàng)目,用到alertDialog,用系統(tǒng)自帶的style很難看,所以查了資料自己定義了個style.
res/value/style.xml內(nèi)增加以下代碼:-->
<style name="dialog" parent="@android:style/Theme.Dialog">
<!--Dialog的windowFrame框?yàn)闊o-->
<item name="android:windowFrame">@null</item>
<!--是否浮現(xiàn)在activity之上-->
<item name="android:windowIsFloating">true</item>
<!--是否半透明-->
<item name="android:windowIsTranslucent">true</item>
<!--是否顯示title-->
<item name="android:windowNoTitle">true</item>
<!--設(shè)置dialog的背景,(#00000000)-->
<item name="android:background">@android:color/transparent</item>
<!--顯示區(qū)域背景是否透明恐锣,(#00000000)-->
<item name="android:windowBackground">@android:color/transparent</item>
<!--就是用來控制灰度的值茅主,當(dāng)為1時,界面除了我們的dialog內(nèi)容是高亮顯示的土榴,dialog以外的區(qū)域是黑色的诀姚,完全看不到其他內(nèi)容,系統(tǒng)的默認(rèn)值是0.5-->
<item name="android:backgroundDimAmount">0.5</item>
<!--顯示區(qū)域以外是否使用黑色半透明背景-->
<item name="android:backgroundDimEnabled">true</item>
</style>
3玷禽、自定義Dialog繼承于Dialog
赫段,并綁定布局
package com.example.administrator.dialogandpopup;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.WindowManager;
/**
* create by Ann
* create on ${DATA}
* description
*/
public class WarningDialog extends Dialog {
public WarningDialog(Context context) {
super(context, R.style.edit_dialog);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_dialog);
WindowManager m = getWindow().getWindowManager();
Display d = m.getDefaultDisplay();
final WindowManager.LayoutParams p = getWindow().getAttributes();
p.height = (int) (d.getHeight() * 0.3);
p.width = (int) (d.getWidth() * 0.8);
getWindow().setAttributes(p);
}
}
4、在活動中顯示Dialog
WarningDialog dialog = new WarningDialog(MainActivity.this);
dialog.show();
自定義Dialog需要設(shè)置單獨(dú)xml
矢赁,style
瑞佩,并把自定的Dialog這個類繼承于Dialog做相應(yīng)的處理,最后在活動中將它顯示出來坯台。
三、一些屬性
progressDialog的屬性
setProgressStyle:設(shè)置進(jìn)度條風(fēng)格瘫寝,默認(rèn)風(fēng)格為圓形蜒蕾,旋轉(zhuǎn)的稠炬。
ProgressDialog.STYLE_SPINNER 環(huán)狀進(jìn)度條
ProgressDialog.STYLE_HORIZONTAL 條狀進(jìn)度條
setTitlt:設(shè)置標(biāo)題
setIcon:設(shè)置標(biāo)題旁邊的圖標(biāo)
setMessage:設(shè)置提示信息
setIndeterminate:設(shè)置ProgressDialog 的進(jìn)度條是否不明確;這個屬性對于ProgressDailog默認(rèn)的轉(zhuǎn)輪模式?jīng)]有實(shí)際意義咪啡,默認(rèn)下設(shè)置為true首启,它僅僅對帶有ProgressBar的Dialog有作用。修改這個屬性為false后可以實(shí)時更新進(jìn)度條的進(jìn)度撤摸。
setCancelable:設(shè)置ProgressDialog 是否可以按返回鍵取消
cancelListner:當(dāng)前Dialog強(qiáng)制取消之后的回調(diào)函數(shù)
setButton:設(shè)置ProgressDialog 的一個Button(需要監(jiān)聽Button事件)
DialogInterface.BUTTON_POSITIVE 最右邊按鈕
DialogInterface.BUTTON_NEGATIVE 最右邊按鈕的左邊一點(diǎn)的按鈕
DialogInterface.BUTTON_NEUTRAL 最左邊按鈕
show:顯示ProgressDialog
cancel:刪除progressdialog,釋放對話框所占的資源
dismiss:刪除progressdialog,釋放對話框所占的資源 作用和cancel相同
hide: 隱藏progressdialog,不會釋放對話框所占的資源
setMax:設(shè)置最大進(jìn)度條的值
getMax:獲取最大進(jìn)度條的值
setProgress:更新進(jìn)度條
getProgress:獲取當(dāng)前進(jìn)度值
incrementProgressBy:增加進(jìn)度值
setProgressDrawable:設(shè)置progress發(fā)生變化時的進(jìn)度指示條的背景圖