简体   繁体   中英

Android How to show numeric softkeyboard with EditText of InputType=“text”

I need allow to user input numeric information in a EditText, but the output needs to be formatted like "##.###,##" (# in [0..9]). The formatting I was made in a TextWatcher... this is good, the TextWatcher does the job... bute, when user selects the EditText, as it marked as text, the AlphaKeyboard is shown, if I select the EditText as numeric the keyboard I need is shown and the TextWatcher stop working.

put the following in your xml at the required edittext

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

refer to How do I show the number keyboard on an EditText in android?

您需要像这样更改edittext条目。

<EditText android:inputType="phone" android:numeric="decimal"></EditText> 

Use this code to show soft keypad.

 InputMethodManager imm = (InputMethodManager) getSystemService(
        Context.INPUT_METHOD_SERVICE);
  imm.showSoftInputFromInputMethod(v.getApplicationWindowToken(), 1);

As you are doing formatting in TextWatcher (##,####....) ie you are adding "," in EditText and you have made the EditText as numeric. If its numeric then "," char is invalid. Thus its not allowing TextWatcher to update the formatted text that contains ',".

You have 3 options to deal with this problem :

  1. Make the EditText as normal instead of numeric. In TextWatcher look out for any other invalid chars other than 0-9. You can show IME for Mumber keypad when the focus is on the EditText.
  2. Use Mask for EditText.
  3. Let it be numeric. Trap FocusGain & FocusLost for the EditText. In focusGain, remove the formatting & set the inputType to Numeric. & In focusLost, first make the input type to Normal and then update the entered value to formmated value. You can set the input type to normal at runtime using setInputType(InputType.TYPE_TEXT_VARIATION_NORMAL); & for numeric use : setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);

I believe 1st option will be best to handle and manage out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM