我想要验证EditTextPreference
。这意味着如果验证失败,我将不得不阻止对话框关闭。我设置了自己的点击监听器,但在解除对话框时没有保存首选项 - 可能是因为不再从对话框构建器处理点击:
public class CustomDialog extends EditTextPreference {
public CustomDialog(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void showDialog(final Bundle state) {
super.showDialog(state);
final AlertDialog d = (AlertDialog) getDialog();
d.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Dismiss
}
});
}
@Override
public void onClick(DialogInterface dialog, int which) {
super.onClick(dialog, which);
}
}
我是否只需手动提交首选项?
编辑:以下是XML:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:title="android custom prefs test">
<com.nerth.vixin.CustomDialog android:defaultValue="@string/address" android:title="IP Address" android:key="key_1"/>
</PreferenceScreen>
答案 0 :(得分:1)
知道了!感谢kha有关onBindDialogView
的提示:
public class CustomDialog extends EditTextPreference {
public CustomDialog(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void showDialog(final Bundle state) {
super.showDialog(state);
final AlertDialog d = (AlertDialog) getDialog();
d.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
persistString("Default");
// Use whatever checks before calling dismiss
d.dismiss();
}
});
}
@Override
protected void onBindDialogView(View view) {
setText(getPersistedString("Default"));
super.onBindDialogView(view);
}
}
答案 1 :(得分:0)
@Override
protected void onPrepareDialogBuilder(Builder builder){
super.onPrepareDialogBuilder(builder)
builder.setCancelable(false)
}