InputMethodManager show numpad in webview

2019-07-20 14:24发布

问题:

I'm using the InputMethodManager to programmatically display the softkeyboard when it's needed

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, 0);

Is there anyway to force the keyboard that appears into being the Number or telephone keypad?

Edit

Sorry I typed this in a hurry before I had to leave my computer for a while. I forgot to mention possibly the most crucial part. I'm trying to do this in a webview. The input type of the textbox i'm focusing on is set to tel. For some reason the numpad won't show up on autofocus which is why I'm trying to force it with InputMethodManager. My current method displays the keyboard focused on the correct field it just doesn't seem to read the type and display correctly.

Thanks for all of your answers so far(which did answer my poorly worded question).

回答1:

Following code will display keypad to you:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, 0);

However if you want to force the keyboard that appears into being the Number or telephone keypad, then you have to set the input type on that particular view/widget.

InputType.TYPE_CLASS_NUMBER; 
InputType.TYPE_CLASS_PHONE;

eg.

your_edit_text.setInputType(InputType.TYPE_CLASS_NUMBER);


回答2:

Use this code for Number :

  <EditText
                    android:id="@+id/editText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@null"
                    android:inputType="number"
                    android:padding="10dp"
                    android:singleLine="true" />

& use this code for Phone :

 <EditText
                    android:id="@+id/editText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@null"
                    android:inputType="phone"
                    android:padding="10dp"
                    android:singleLine="true" />

For Programmatically try this

password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);

Where password is edittext.

This will Work.



回答3:

a) in xml

android:inputType="number"

b) in code

EditText editView = new EditText(this);
editView.setKeyListener(new DigitsKeyListener());