Java 从自定义对话框首选项和持久值中检索值
正在尝试编写自定义对话框首选项(aJava 从自定义对话框首选项和持久值中检索值,java,android,dialog-preference,Java,Android,Dialog Preference,正在尝试编写自定义对话框首选项(aNumberPickerDialog)。虽然android文档详细介绍了这个主题,但我似乎忽略了他们文档中的一些基本构建块 到目前为止,我拥有的是一个自定义首选项对话框,它显示在“设置”活动中。我可以单击首选项并填写一个值,然后按OK/Cancel 自定义布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.c
NumberPickerDialog
)。虽然android文档详细介绍了这个主题,但我似乎忽略了他们文档中的一些基本构建块
到目前为止,我拥有的是一个自定义首选项对话框,它显示在“设置”活动中。我可以单击首选项并填写一个值,然后按OK/Cancel
自定义布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/editTextNumberPickerValue"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Preference.xml扩展为:
<com.cleancode.utils.numberpickerpreference.NumberPickerPreference
android:defaultValue="550"
android:key="prefLongFlashDuration"
android:summary="@string/long_flash_duration_summary"
android:title="@string/long_flash_duration" />
我怎样才能:
- 是否实际显示默认值550
- 是否从对话框中检索值
- 强制只输入整数值
非常感谢。在谷歌的文档中,他们提到了包含 新价值 在本例中,
mNewValue
是保存设置当前值的类成员
因此,您似乎必须依赖成员字段来跟踪包含新值的视图。我就是这样解决的
private EditText newPasswordField;
@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
newPasswordField = view.findViewById(R.id.new_password_field);
}
因此,基本上您重写了一个方法,该方法允许您保留对保存新设置值的任何元素的引用
然后你做你已经拥有的事情:提取新的值
@Override
protected void onDialogClosed(boolean positiveResult) {
Log.i(TAG, "Password dialog closed. Result = " + positiveResult);
if (positiveResult) {
persistString(newPasswordField.getText().toString());
}
}
下面的链接回答了答案依赖于
findIndexOfValue(getValue())
检索所选值的问题。该方法在对话框首选项中不可用,因为它没有意义
@Override
protected void onDialogClosed(boolean positiveResult) {
Log.i(TAG, "Password dialog closed. Result = " + positiveResult);
if (positiveResult) {
persistString(newPasswordField.getText().toString());
}
}