开发者

Input type for EditText in preference screen?

开发者 https://www.devze.com 2023-03-10 12:11 出处:网络
Is there any way to set an EditText 开发者_开发知识库in my XML preference screen to only accept number input?In the xml file:

Is there any way to set an EditText 开发者_开发知识库in my XML preference screen to only accept number input?


In the xml file:

android:inputType="number" 

or during runtime:

editText.setRawInputType(TYPE_CLASS_NUMBER | TYPE_NUMBER_VARIATION_NORMAL);

http://developer.android.com/reference/android/widget/TextView.html#attr_android:inputType


EditTextPreference supports all the same XML attributes as does an EditText. So, use android:inputType="number"


Everyone is facing this issue because eclipse is not showing hint when we press the ctrl+space.

Just copy

android:inputType="number"

and paste. It will work like a charm


Fully agree with the answer. However I would like to add something here:Once you accept numeric input there is still a scenario where you may get error i.e when a user clicks submit on a blank EditText. Since you will in all probability be parsing your input to an int or double or float. A blank input will throw an error. I suggest you should check for error using try catch and NumberFormatException.


 <EditTextPreference
    android:inputType="phone"
    android:key="phone_number"
    android:summary="@string/settings_phone_number_sum"
    android:title="@string/settings_phone_number" />


I enclose reading preference is try/catch NumberFormatException. If exception, I return default value.

I found it useful when migrating app from say float to int type preferences. Previous installations had float for preference and new one used int.

Of course one still needs to do validation on user entry.

0

精彩评论

暂无评论...
验证码 换一张
取 消