Android Display Numeric Keypad on Button Click

为君一笑 提交于 2019-12-21 05:16:09

问题


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"


来源:https://stackoverflow.com/questions/11134873/android-display-numeric-keypad-on-button-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!