setInputType on EditTextPreference

僤鯓⒐⒋嵵緔 提交于 2020-01-03 08:47:06

问题


how can i add the "setInputType" propety to an EditTextPreference (my goal is to set the input type to numbers only), i've tried:

editTextPref.setInputType(InputType.TYPE_CLASS_NUMBER);

but this only seems to work for EditTexts, not EditTextPreferences


回答1:


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());



回答2:


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);



回答3:


Try This

  1. call getEditText() from your editTextPref
  2. then set input type to that.


来源:https://stackoverflow.com/questions/7319729/setinputtype-on-edittextpreference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!