Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set EditText Digits Programmatically

I am essentially trying to set the digits value of an EditText programmatically. So far I have:

weightInput.setInputType(InputType.TYPE_CLASS_PHONE); weightInput.setKeyListener(DigitsKeyListener.getInstance()); 

Which is fine, but I also want to be able to include a decimal place (.). Any ideas?

like image 492
ryandlf Avatar asked Sep 04 '11 16:09

ryandlf


2 Answers

Try this:

<EditText     android:inputType="number"     android:digits="0123456789." /> 

From Code:

weightInput.setKeyListener(DigitsKeyListener.getInstance("0123456789.")); 

But, it allows the user to include several "." See JoeyRA's answer for real numbers.

like image 192
Whiler Avatar answered Sep 20 '22 04:09

Whiler


Try this:

weightInput.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);            weightInput.setKeyListener(DigitsKeyListener.getInstance(false,true)); 

public static DigitsKeyListener getInstance (boolean sign, boolean decimal)  

Returns a DigitsKeyListener that accepts the digits 0 through 9, plus the minus sign (only at the beginning) and/or decimal point (only one per field) if specified.

This solve the problem about the many '.' in EditText

like image 41
JoeyRA Avatar answered Sep 19 '22 04:09

JoeyRA