Set EditText Program Codes

I am essentially trying to programmatically set a digit value in an EditText. So far I:

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

This is good, but I also want to be able to use decimal place (.). Any ideas?

+55
android android-edittext
Sep 04 '11 at 16:11
source share
4 answers

Try the following:

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

From the code:

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

But, this allows the user to include a few "." See Muffinbubble Answer for real numbers.

+122
Sep 04 '11 at 16:16
source share

Try the following:

 weightInput.setInputType(InputType.TYPE_CLASS_NUMBER); 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 digits from 0 to 9, plus a minus sign (only at the beginning) and / or a decimal point (only one per field), if specified.

This solves the problem of many ".". in EditText

+21
Feb 06 '14 at 12:37
source share

Use InputType.TYPE_NUMBER_FLAG_DECIMAL .

Also see: Types of input data .

+19
Sep 04 '11 at 16:32
source share

To enter an IP address (multiple dots and numbers)

to try

 <EditText android:id="@+id/ipBox" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/ipAddrHint" android:inputType="numberDecimal|number" android:digits="0123456789." android:textSize="30sp" /> 
+3
Dec 23 '12 at 15:41
source share



All Articles