Set EditText Digits Programmatically

前端 未结 4 1740
孤城傲影
孤城傲影 2020-11-27 15:03

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

weightInput.setInputType(InputType.TYPE_CLASS_PHONE);
weightI         


        
相关标签:
4条回答
  • 2020-11-27 15:36

    Use InputType.TYPE_NUMBER_FLAG_DECIMAL.

    Also see: Input Types.

    0 讨论(0)
  • 2020-11-27 15:39

    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.

    0 讨论(0)
  • 2020-11-27 15:42

    For IP address input ( Multiple dots and numbers )

    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" />
    
    0 讨论(0)
  • 2020-11-27 15:57

    Try this:

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

    0 讨论(0)
提交回复
热议问题