当用户单击editText时,键盘将被打开供用户键入。当用户单击按钮而不是editText时,我可以使键盘出现吗?numpad能代替普通键盘出现吗?
发布于 2018-07-21 21:55:53
当用户单击按钮而不是editText时,我可以使键盘出现吗?
是的,您需要设置焦点并使用InputMethodManager
弹出键盘。
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Focus the field.
editText.requestFocus();
// Show soft keyboard for the user to enter the value.
InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
im.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
});
numpad能代替普通键盘出现吗?
是的,使用输入类型
在edittext
的xml标记中
<EditText...
android:inputType="number"/>
或用java
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
发布于 2018-07-21 22:04:56
public static void toggleKeyboard(Context context) {
try {
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
} catch (Exception e) {
Log.e("On show keyboard error: %s", e.getMessage());
}
}
https://stackoverflow.com/questions/51460360
复制相似问题