正在尝试编写自定义对话框首选项(NumberPickerDialog
)。尽管 android 文档详细介绍了这个主题,但我似乎错过了他们文档中的一些基本构建块。
到目前为止,我有一个自定义首选项对话框,显示在设置活动中。我可以单击首选项并填写一个值,然后按确定/取消。
自定义布局:
<?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>
NumberPickerDialog(正在进行中...):
public class NumberPickerPreference extends DialogPreference {
public NumberPickerPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setDialogLayoutResource(R.layout.numberpicker_dialog);
setPositiveButtonText(android.R.string.ok);
setNegativeButtonText(android.R.string.cancel);
setDialogIcon(null);
setPersistent(false);
}
@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
}
@Override
protected void onDialogClosed(boolean positiveResult) {
if (positiveResult) {
Editor editor = getEditor();
persistInt( value??? );
}
}
}
首选项.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?
- 从对话框中检索值?
- 强制只输入整数值?
我希望有人能对此有所了解,可怜的Android文档在这个主题上并不是新手友好的。
非常感谢。
在谷歌的文档中,他们对包含 新价值
在此示例中,
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());
}
}