简体   繁体   English

如何显示android数字键盘?

[英]How to display android numeric keyboard?

I want to display android keyboard when fragment starts. 我想在片段开始时显示android键盘。

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

I use this code and it works. 我使用此代码,它可以工作。 But how to change the keyboard to numeric keyboard? 但是如何将键盘更改为数字键盘? number only. 仅数字。 And how to listen to the keyboard event like "1" on keyboard is pressed? 以及如何监听键盘事件,例如按下键盘上的“ 1”?

Hopping to help someone to get stuck in that this is my solution. 希望帮助某人陷入困境,这是我的解决方案。

(even know that this is not the exact question I get in this page a lot of times because of the way that the question was made) (甚至知道由于问题的产生方式,这并不是我在此页面中多次提出的确切问题)

Based on this solution Soft Keyboard for Dialog and Activity I get answer to show the Numeric Keyboard inside a Fragment when open the Screen . 基于此解决方案, 用于对话框和活动的软键盘 打开屏幕时,我得到的答案是在片段内显示数字键盘

First you have to be sure that your element in .XML file is number type 首先,您必须确保.XML文件中的元素是数字类型

android:inputType="number"

Then, inside your code you do this: 然后,在代码内部执行以下操作:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    fragmentView = super.onCreateView(inflater, container, savedInstanceState);
    getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

    return fragmentView;
}

And don't forget to focus your element, like this 而且不要忘了像这样集中您的元素

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    myElement.requestFocus();
}

You'd need to create an InputConnection on whatever view you have bound the keyboard to, and return it from that view's onCreateInputConnection function. 您需要在将键盘绑定到的任何视图上创建InputConnection,然后从该视图的onCreateInputConnection函数返回它。 When you set up the EditorInfo output parameter, set the type to numeric. 设置EditorInfo输出参数时,请将类型设置为数字。 To listen for key presses, override the commitText function and handle it. 要监听按键,请覆盖commitText函数并进行处理。 You'll also have to deal with the possibility of composing text being set instead of commitText being initially called. 您还必须处理设置文本而不是最初调用commitText的可能性。 It is really NOT recommended to do custom keyboard handling, its extremely complex. 确实不建议您进行自定义键盘处理,因为它非常复杂。 You're better off creating your own "keypad" with 9 numbers on it as buttons and using onClickHandlers. 最好创建自己的带有9个数字的“键盘”作为按钮,并使用onClickHandlers。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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