如何显示android数字键盘?

时间:2016-01-28 22:34:24

标签: android

我想在片段启动时显示android键盘。

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

我使用此代码并且它有效。但是如何将键盘更改为数字键盘?仅限数字。以及如何听键盘事件,如" 1"在键盘上按下了吗?

2 个答案:

答案 0 :(得分:1)

跳跃帮助别人陷入困境,这是我的解决方案。

(甚至知道这不是我在这个页面中多次出现的问题,因为问题的方式已经存在)

基于此解决方案Soft Keyboard for Dialog and Activity我得到答案在打开屏幕时在片段中显示数字键盘

首先,您必须确保.XML文件中的元素是数字类型

android:inputType="number"

然后,在代码中执行此操作:

@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;
}

不要忘记关注你的元素,比如

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

答案 1 :(得分:0)

您需要在键盘绑定到的任何视图上创建一个InputConnection,并从该视图的onCreateInputConnection函数返回它。设置EditorInfo输出参数时,请将类型设置为numeric。要监听按键,请覆盖commitText函数并处理它。您还必须处理组合正在设置的文本而不是最初调用commitText的可能性。真的不建议做自定义键盘处理,它非常复杂。你最好创建自己的键盘"使用9个数字作为按钮并使用onClickHandlers。