我有一些问题需要理解为什么这个,一般来说,名为Material Dialogs的伟大图书馆让我失望:
private MaterialDialog wifiDialog;
@Override
public void showWiFiDialog() {
if (wifiDialog != null) {
wifiDialog.dismiss();
wifiDialog = null;
}
wifiDialog = new MaterialDialog.Builder(this)
.title("Please activate WiFi")
.contentColor(Color.BLACK)
.content(R.string.wifi_dialog_content)
.positiveText("Settings")
.positiveColor(Color.BLACK)
.negativeText("Exit")
.negativeColor(Color.BLACK)
.cancelable(false)
.onPositive((dialog, which) -> startActivityForResult(new Intent(ACTION_WIFI_SETTINGS), REQUEST_CHECK_SETTINGS))
.onNegative((dialog, which) -> finish())
.build();
wifiDialog.show();
}
@Override
public void hideWifiDialog() {
if (wifiDialog != null) {
wifiDialog.dismiss();
wifiDialog = null;
}
}
致电:
总结:第一次调用show和hide时,一切都按预期工作。无论何时多次调用它,行为都变得不确定。我想知道为什么?
答案 0 :(得分:0)
我认为,每次要显示时,都不需要重新初始化MaterialDialog
。让我们看看下面的代码是否有所不同。
private MaterialDialog wifiDialog;
@Override
public void showWiFiDialog() {
if (wifiDialog != null) {
wifiDialog.dismiss();
}else{
wifiDialog = new MaterialDialog.Builder(this)
.title("Please activate WiFi")
.contentColor(Color.BLACK)
.content(R.string.wifi_dialog_content)
.positiveText("Settings")
.positiveColor(Color.BLACK)
.negativeText("Exit")
.negativeColor(Color.BLACK)
.cancelable(false)
.onPositive((dialog, which) -> startActivityForResult(new Intent(ACTION_WIFI_SETTINGS), REQUEST_CHECK_SETTINGS))
.onNegative((dialog, which) -> finish())
.build();
}
wifiDialog.show();
}
@Override
public void hideWifiDialog() {
if (wifiDialog != null) {
wifiDialog.dismiss();
}
}