Android Support Input Type EditTextPreference

Can I specify the type of input method for android.support.v7.preference.EditTextPreference ?

+8
source share
3 answers

Now you can use Android-Support-Preference-V7-Fix .
Fixed EditTextPreference sends XML attributes (e.g. inputType ) to EditText , like the original preference.

+3
source

Edit: The previous answers below were built in stock android.preference.EditTextPreference and unfortunately do not work for android.support.v7.preference.EditTextPreference .

In android.preference.EditTextPreference , the EditText control is created programmatically and the AttributeSet from the Preference is passed to it.

android.preference.EditTextPreference Source:

 public EditTextPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); mEditText = new EditText(context, attrs); // Give it an ID so it can be saved/restored mEditText.setId(com.android.internal.R.id.edit); /* * The preference framework and view framework both have an 'enabled' * attribute. Most likely, the 'enabled' specified in this XML is for * the preference framework, but it was also given to the view framework. * We reset the enabled state. */ mEditText.setEnabled(true); } 

White allows us to set inputType on the Preference itself and pass it to EditText . Unfortunately android.support.v7.preference.EditTextPreference creates an EditText in Layout

See this question for ideas on working on this:

I just want to tell you that the subclass is EditTextPreferenceDialogFragment and override onAddEditTextToDialogView, as well as overriding PreferenceFragmentCompat # onDisplayPreferenceDialog to show that the subclass works fine as needed, thanks for the help.


Create your own class that extends EditTextPreference and sets it there.

Here is my EditIntegerPreference class:

 public class EditIntegerPreference extends EditTextPreference { public EditIntegerPreference(Context context) { super(context); } public EditIntegerPreference(Context context, AttributeSet attributeSet) { super(context, attributeSet); } public EditIntegerPreference(Context context, AttributeSet attributeSet, int defStyle) { super(context, attributeSet, defStyle); getEditText().setInputType(InputType.TYPE_CLASS_NUMBER); getEditText().setSelectAllOnFocus(true); } @Override public String getText() { try { return String.valueOf(getSharedPreferences().getInt(getKey(), 0)); } catch (Exception e) { return getSharedPreferences().getString(getKey(), "0"); } } @Override public void setText(String text) { try { if (getSharedPreferences() != null) { getSharedPreferences().edit().putInt(getKey(), Integer.parseInt(text)).commit(); } } catch (Exception e) { // TODO: This catch stinks! } } @Override protected void onSetInitialValue(boolean restoreValue, Object defaultValue) { getEditText().setInputType(InputType.TYPE_CLASS_NUMBER); getEditText().setSelectAllOnFocus(true); if (restoreValue) { getEditText().setText(getText()); } else { super.onSetInitialValue(restoreValue, defaultValue != null ? defaultValue : ""); } } } 

Note that you can add the inputType attribute to EditTextPreference

 android:inputType="number" 

The reason I didn’t choose this route is because I wanted my preference to be saved as Integer , not String

+3
source

You need to add a custom layout and specify an EditText with android: inputType = "number"

 <android.support.v7.preference.EditTextPreference android:dialogLayout="@layout/preference_dialog_edittext_custom" 

So that you can copy the source file preference_dialog_layout.xml and edit it.

 <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="48dp" android:layout_marginBottom="48dp" android:overScrollMode="ifContentScrolls"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="24dp" android:layout_marginEnd="24dp" android:orientation="vertical"> <TextView android:id="@android:id/message" style="?android:attr/textAppearanceSmall" android:layout_marginBottom="48dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="?android:attr/textColorSecondary" /> <EditText android:id="@android:id/edit" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="number" android:layout_marginStart="-4dp" android:layout_marginEnd="-4dp" /> </LinearLayout> </ScrollView> 
0
source

Source: https://habr.com/ru/post/1241055/


All Articles