Android 弹窗

弹出和隐藏进度条

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
private Dialog dialog;

private void showDialog(final Context ctx) {
if (ctx instanceof Activity) {
final Activity context = (Activity) ctx;
context.runOnUiThread(new Runnable() {
@Override
public void run() {
int spacing = Uscreen.dp2Px(context, 16);
LinearLayout layout = new LinearLayout(context);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(layoutParams);
layout.setGravity(Gravity.CENTER);
layout.setPadding(spacing, spacing, spacing, spacing);
// add ProgressBar
ProgressBar bar = new ProgressBar(context);
layout.addView(bar, layoutParams);

dialog = new Dialog(context, android.R.style.Theme_Holo_Dialog_NoActionBar);
dialog.setCancelable(false);
dialog.getWindow().setDimAmount(0.3f);
dialog.setContentView(layout);
dialog.show();
}
});
}

}

private void dismissDialog() {
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
}

单个操作的 alert

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
public class AlertOneItem {
private Context context;
private String title;
private String message;
private Consumer<Boolean> consumer;
private String positiveTips = "确定";
private String negativeTips = "取消";

private AlertOneItem(Context context, Consumer<Boolean> consumer) {
this.context = context;
this.consumer = consumer;
}

public static AlertOneItem builder(Context context, Consumer<Boolean> consumer) {
return new AlertOneItem(context, consumer);
}

public AlertOneItem title(String title) {
this.title = title;
return this;
}

public AlertOneItem message(String message) {
this.message = message;
return this;
}

public AlertOneItem positiveTips(String positiveTips) {
this.positiveTips = positiveTips;
return this;
}

public AlertOneItem negativeTips(String negativeTips) {
this.negativeTips = negativeTips;
return this;
}


public void show() {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
if (!TextUtils.isEmpty(title)) {
builder.setTitle(title);
}
if (!TextUtils.isEmpty(message)) {
builder.setMessage(message);
}
if (!TextUtils.isEmpty(negativeTips)) {
builder.setNegativeButton(negativeTips, (dialog, which) -> consumer.accept(false));
}

if (!TextUtils.isEmpty(positiveTips)) {
builder.setPositiveButton(positiveTips, (dialog, which) -> consumer.accept(true));
}

builder.setCancelable(true);
builder.create();
builder.show();
}
}
// 调用
Udialog.AlertOneItem.builder(context, result -> {
if (result) {
toSetWallpaperAsync(context, imageView, color);
}
}).message("设置成壁纸?").show();

多个操作的 alert (方案 1)

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
42
43
44
45
46
47
48
49
50
51
52
53
enum OperateType {
REFRESH("刷新"),
COPY_LINK("复制链接"),
COPY_MD_LINK("复制md链接"),
OPEN_WITH_BROWSER("在浏览器中打开"),
;
String title;

OperateType(String title) {
this.title = title;
}

public static OperateType indexOf(int index) {
return OperateType.values()[index];
}

public static String[] toStrArray() {
OperateType[] values = OperateType.values();
String[] result = new String[values.length];
for (int i = 0; i < values.length; i++) {
result[i] = values[i].title;
}
return result;
}
}

private void onMorePressed() {
new AlertDialog.Builder(mContext)
.setTitle("操作")
.setItems(OperateType.toStrArray(), (dialog, which) -> {
switch (OperateType.indexOf(which)) {
case REFRESH:
mWvContent.reload();
break;
case COPY_LINK:
copyToClipboard(mWvContent.getUrl(), "已复制到剪切板");
break;
case COPY_MD_LINK:
String title = mWvContent.getTitle();
String url = mWvContent.getUrl();
copyToClipboard("- [" + title + "]" + "(" + url + ")", "已复制到剪切板");
break;
case OPEN_WITH_BROWSER:
Uri uri = Uri.parse(mWvContent.getUrl());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
break;
default:
}
})
.create().show();
}

多个操作的 alert(方案 2)

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
42
43
44
45
46
47
48
49
50
public class AlertMultiItem {
private List<String> nameList = new ArrayList<>();
private List<Runnable> taskList = new ArrayList<>();
private Context context;
private String title;

private AlertMultiItem(Context context) {
this.context = context;
}

public static AlertMultiItem builder(Context context) {
return new AlertMultiItem(context);
}

public AlertMultiItem title(String title) {
this.title = title;
return this;
}

public AlertMultiItem add(String title, Runnable runnable) {
nameList.add(title);
taskList.add(runnable);
return this;
}

private String[] getItemNames() {
String[] result = new String[nameList.size()];
for (int i = 0; i < nameList.size(); i++) {
result[i] = nameList.get(i);
}
return result;
}

public void show() {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
if (!TextUtils.isEmpty(title)) {
builder.setTitle(title);
}
builder.setItems(getItemNames(), (dialog, which) -> taskList.get(which).run());
builder.create();
builder.show();
}
}

// 调用
Udialog.AlertMultiItem.builder(mContext)
.add("复制内容", () -> copyContent())
.add("复制全部", () -> copyAll())
.add("删除此项", () -> delete())
.show();