一.代码位置

  • XTester-Dialog
    此文是在此基础上做的讲解,用于加深印象

二.普通对话框

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**普通对话框**/
private void showNormalDialog(){
final AlertDialog.Builder normalDialog = new AlertDialog.Builder(this);
normalDialog.setIcon(R.mipmap.tr_step_left_normal);
normalDialog.setTitle("DiaLog Title");
normalDialog.setMessage("DiaLog Message");
normalDialog.setPositiveButton("Positive",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showToast("Positive");
}
});
normalDialog.setNegativeButton("Negative",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showToast("Negative");
}
});
normalDialog.show();
}

四.普通对话框 增加第三个选择按钮

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**普通对话框 增加第三个选择按钮**/
private void showMultiBtnDialog(){
AlertDialog.Builder normalDialog =
new AlertDialog.Builder(MainActivity.this);
normalDialog.setIcon(R.mipmap.tr_step_left_normal);
normalDialog.setTitle("DiaLog Title").setMessage("DiaLog Message");
normalDialog.setPositiveButton("Positive",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showToast("Positive");
}
});
normalDialog.setNeutralButton("Neutral2",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showToast("Neutral2");
}
});
normalDialog.setNegativeButton("Negative", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showToast("Negative");
}
});
// 创建实例并显示
normalDialog.show();
}

五.列表选择对话框

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**列表选择对话框**/
private void showListDialog() {
final String[] items = { "items1","items2","items3","items4" };
AlertDialog.Builder listDialog =
new AlertDialog.Builder(MainActivity.this);
listDialog.setTitle("DiaLog Title");
listDialog.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showToast("点击 "+items[which]);
}
});
listDialog.show();
}

六.列表单选对话框

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**列表单选对话框**/
int mChoice;
private void showSingleChoiceDialog(){
final String[] items = { "items1","items2","items3","items4" };
AlertDialog.Builder singleChoiceDialog =
new AlertDialog.Builder(MainActivity.this);
singleChoiceDialog.setTitle("DiaLog Title");
// 第二个参数是默认选项,此处设置为0
singleChoiceDialog.setSingleChoiceItems(items, mChoice,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mChoice = which;
}
});
singleChoiceDialog.setPositiveButton("Positive",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (mChoice != -1) {
showToast("选择 "+items[mChoice]);
}
}
});
singleChoiceDialog.show();
}

七.列表多选对话框

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**列表多选对话框**/
ArrayList<String> mChoices = new ArrayList<>();
final boolean initChoiceSets[]={false,false,false,false};
private void showMultiChoiceDialog() {
final String[] items = { "items1","items2","items3","items4" };
AlertDialog.Builder multiChoiceDialog = new AlertDialog.Builder(this);
multiChoiceDialog.setTitle("DiaLog Title");
multiChoiceDialog.setMultiChoiceItems(items, initChoiceSets,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
initChoiceSets[which] = isChecked;
if (isChecked) {
mChoices.add(items[which]);
} else {
String s = items[which];
Log.i("md"," findInterIdex(mChoices,s): "+findInterIdex(mChoices,s));
mChoices.remove(findInterIdex(mChoices,s));
}
}
});
multiChoiceDialog.setPositiveButton("Positive",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int size = mChoices.size();
String str = "";
for (int i = 0; i < size; i++) {
str += mChoices.get(i) ;
}


showToast("选择了 "+str);
}
});
multiChoiceDialog.show();
}
public static int findInterIdex(List<String> nums, String target){
return Collections.binarySearch(nums,target);
}

八.进度弹出框

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**进度弹出框**/
private void showProgressDialog() {
final int MAX_PROGRESS = 100;
final ProgressDialog progressDialog =
new ProgressDialog(MainActivity.this);
progressDialog.setProgress(0);
progressDialog.setTitle("ProgressDialog Title");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMax(MAX_PROGRESS);
progressDialog.show();
/* 模拟进度增加的过程
* 新开一个线程,每个100ms,进度增加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();
}
}
// 进度达到最大值后,窗口消失
progressDialog.cancel();
}
}).start();
}

九.输入对话框

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**输入对话框**/
private void showInputDialog() {
final EditText editText = new EditText(MainActivity.this);
AlertDialog.Builder inputDialog =
new AlertDialog.Builder(MainActivity.this);
inputDialog.setTitle("AlertDialog Title").setView(editText);
inputDialog.setPositiveButton("Positive",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showToast("输入了 "+editText.getText().toString());
}
}).show();
}

十.自定义输入对话框

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**自定义输入对话框
private void showCustomizeDialog() {
AlertDialog.Builder customizeDialog =
new AlertDialog.Builder(MainActivity.this);
final View dialogView = LayoutInflater.from(MainActivity.this)
.inflate(R.layout.dialog_customize,null);
customizeDialog.setTitle("自定义AlertDialog Title");
customizeDialog.setView(dialogView);
dialogView.findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showToast("输入了 "+((EditText)dialogView.findViewById(R.id.edit_text)).getText().toString());
}
});
customizeDialog.show();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!-- 自定义View -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
>

<EditText
android:id="@+id/edit_text"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
/>

<ImageView
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/tr_step_left_normal"
/>
</LinearLayout>

十一:二级对话选择框

二级选择框使用笔记