Trying to write a custom dialog preference (a NumberPickerDialog
). Although the android documentation details on this topic, I seem to miss some essential building blocks in their documentation.
What I have so far is a custom preference dialog that shows up in the settings activity. I can click on the preference and fill in a value and press OK/Cancel.
The custom layout:
<?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 (work in progress...):
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??? );
}
}
}
Preference.xml extended with:
<com.cleancode.utils.numberpickerpreference.NumberPickerPreference
android:defaultValue="550"
android:key="prefLongFlashDuration"
android:summary="@string/long_flash_duration_summary"
android:title="@string/long_flash_duration" />
How can I:
I hope somebody can shed some light on this, pitty Android documentation is not a little newby friendly on this topic.
Many thanks.
On Google's doc they say this about the variable that contains the new value
In this example,
mNewValue
is a class member that holds the setting's current value.
So it seems you have to rely on a member field to keep track of the view that contains your new value. This is the way I solved it
private EditText newPasswordField;
@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
newPasswordField = view.findViewById(R.id.new_password_field);
}
So basically you override a method that will let you keep a reference to whatever element holds your new setting's value
And then you do what you already had: extract the new value
@Override
protected void onDialogClosed(boolean positiveResult) {
Log.i(TAG, "Password dialog closed. Result = " + positiveResult);
if (positiveResult) {
persistString(newPasswordField.getText().toString());
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.