how can i add the "setInputType" propety to an EditTextPreference (my goal is to set the input t开发者_JAVA百科ype to numbers only), i've tried:
editTextPref.setInputType(InputType.TYPE_CLASS_NUMBER);
but this only seems to work for EditTexts, not EditTextPreferences
You can retrieve the EditText from the Preference and from there setInputTypes or use KeyListeners to inform the keyboard:
EditText et = (EditText) editTextPref.getEditText();
et.setKeyListener(DigitsKeyListener.getInstance());
If all you really need is a single inputType for your editTextPreference you could set it in the XML with android:inputType="number" or android:inputType="numberDecimal".
Example:
<EditTextPreference
android:defaultValue="130"
android:dialogMessage="@string/upper_limit_hint"
android:dialogTitle="@string/upper_limit_text"
android:inputType="number"
android:key="UPPER_LIMIT"
android:maxLength="4"
android:summary="@string/upper_limit_hint"
android:title="@string/upper_limit_text" />
Also, thanx to Dave's suggestion above, I was able to achieve this programmatically by assigning the EditTextPreference to a TextView (I was unable to use an EditText).
Example:
EditTextPreference editTextPreference = (EditTextPreference) preference;
TextView etpTextView = (TextView) editTextPreference.getEditText();
// If I want entry with decimal capability
etpTextView.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
// or if I want entry without decimal capability
etpTextView.setInputType(InputType.TYPE_CLASS_NUMBER);
Try This
- call getEditText() from your editTextPref
- then set input type to that.
精彩评论