Android Display Numeric Keypad on Button Click

2019-03-30 10:47发布

问题:

In my application, I am trying to display the numeric keypad when the user clicks on a button.

When the button is clicked, I shift the focus to the EditText in my layout using requestFocus() and next I need to display the numeric keypad so that the user can type in the values..

The values will always be numeric and hence I need to show only the numeric keypad.

I tired using this inside my button's onClick() method but it does not work.

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);

Please provide me with any solution to this.

Also, my application is for an Android tablet supporting 4.0.3.

回答1:

This one in your EditText property

android:inputType="phone" (This will displayed phone numeric keypad)

or

android:inputType="number" (This will displayed numeric keypad)

Now, you have to just set Focus on your EditText on Button's click..

something like,

edtNumber = (EditText) findViewById(R.id.number);

// Button's onClick....
@Override
 public void onClick(DialogInterface dialog, int which)
  {
    edtNumber.requestFocus();
  }


回答2:

in EditText put below line.

android:inputType="number"