As you will notice, the setting will still be saved as a string in the
preference file.
To avoid this, I subclassed EditTextPreference into
EditIntegerPreference.
Here is my "as far as I tested working" class:
package some.package;
import android.content.Context;
import android.preference.EditTextPreference;
import android.util.AttributeSet;
public class EditIntegerPreference extends EditTextPreference
{
public EditIntegerPreference(Context context)
{
super(context);
}
public EditIntegerPreference(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public EditIntegerPreference(Context context, AttributeSet attrs,
int defStyle)
{
super(context, attrs, defStyle);
}
@Override
public String getText()
{
return String.valueOf(getSharedPreferences().getInt(getKey(), 0));
}
@Override
public void setText(String text)
{
getSharedPreferences().edit().putInt(getKey(),
Integer.parseInt(text))
.commit();
}
@Override
protected void onSetInitialValue(boolean restoreValue, Object
defaultValue)
{
if (restoreValue)
getEditText().setText(getText());
else
super.onSetInitialValue(restoreValue, defaultValue);
}
}
In the XML you use it with the full class name, e.g.
<some.package.EditIntegerPreference.
Hope that will help.
On 29 sep, 15:02, Guillaume Perrot <[EMAIL PROTECTED]> wrote:
> Short answer:
> <EditTextPreference
> android:key="distance_units_m"
> android:title="Distance"
> android:summary="Summary"
> android:dialogTitle="title"
> android:numeric="integer"
> android:maxLength="4"
> android:hint="Enter distance (max 9999)" />
> "This EditText can be modified [...] through XML by setting any
> EditText attributes on the EditTextPreference."
>
> Full answer:
> When using the dialogLayout attribute, maybe a special android:id
> identifier is required like the ids that must be used in ListActivity
> or TabActivity.
> Plus the documentation warns us about that attribute, as for the
> layout attribute (use the widgetLayout attribute instead).
>
> On 26 sep, 15:27, Ludwig <[EMAIL PROTECTED]> wrote:
>
> > I have a preference setting that I need to customize so that the preference
> > will only accept ints.
> > The way I understand this is that I specify the android:dialogLayout with
> > the name of my custom layout:
> > <EditTextPreference
> > android:key="distance_units_m"
> > android:title="Distance"
> > android:summary="Summary"
> > android:dialogTitle="title"
> > android:dialogLayout="@layout/preferences_distance"
> > />
>
> > This is my customized EditText: it only accepts numbers up to 9999.
> > <?xml version="1.0" encoding="UTF-8"?>
>
> > <EditText
> > xmlns:android="http://schemas.android.com/apk/res/android"
> > android:numeric="integer"
> > android:maxLength="4"
> > android:hint="Enter distance (max 9999)"
> > />
> > This works and shows up correctly.
>
> > I do not do anything else and my activity simply loads the XML resource.
>
> > The problem is that if I do specify my custom editor the data from the user
> > is not automatically saved anymore.
> > (Removing only the line with the dialogLayout makes it save as expected, so
> > that is correct)
>
> > Do I have to specify anything else in the XML to get it saved? Or do I now
> > have to program this?
> > (I would have thought that behind the scenes, the editor (whether it is
> > custom or not) is somehow created by a factory, and then all invocations
> > happen on the created object, thus making it no difference how the look/feel
> > of the custom editor is. But that is obviously not how it is done)
>
> > Any pointers?
>
> > Ludwig
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---