【问题标题】:Change keyboard's text pad with number pad用数字键盘更改键盘的文本键盘
【发布时间】:2017-10-02 06:05:18
【问题描述】:

我有 EditText,输入类型是 textNoSuggestions。

 <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="16dp"
                    android:layout_marginTop="5dp">

                    <EditText
                        android:id="@+id/firstName"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:ems="10"
                        android:hint="Name"
                        android:inputType="textNoSuggestions" />
                </android.support.design.widget.TextInputLayout>

我还有一个按钮,在按钮 onclick 方法中我尝试更改键盘的输入类型。这是一个来源

 final Button changeKeyboard = (Button) dialog.findViewById(R.id.change_keyboard);
    changeKeyboard.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            firsName.setRawInputType(InputType.TYPE_CLASS_NUMBER);

        }
    });

当键盘显示时,是否可以在单击按钮时更改键盘的输入类型? 我该如何解决我的问题谢谢大家

【问题讨论】:

  • 这似乎是一个独特的问题,搜索SO后,我找不到类似的问题。祝你好运找到答案。

标签: android android-edittext android-button android-keypad


【解决方案1】:

set 是 setTransformationMethod(),而不是 setInputType()。所以像:

firstName.setTransformationMethod(numberTransformationMethod.getInstance());

【讨论】:

  • 也许你不明白我的问题。我想在点击按钮时用数字键盘更改键盘的文本键盘(当键盘显示时)@jai khambhayta
【解决方案2】:

在您的代码上,更改:

firsName.setRawInputType(InputType.TYPE_CLASS_NUMBER);

到这个(名字带有“t”,你的xml上的名字):

firstName.setRawInputType(InputType.TYPE_CLASS_NUMBER);

另外,您也可以通过调用setInputType 更改您的键盘,如下所示:firstName.setInputType(x),其中 x 是一个 int,可以是 1 (字母数字); 2(数字)或 3(电话)。

编辑:

你可以隐藏你的键盘在你的活动中调用这个:

public static void hideKeyboard(Activity activity) {
    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
    //Find the currently focused view, so we can grab the correct window token from it.
    View view = activity.getCurrentFocus();
    //If no view currently has focus, create a new one, just so we can grab a window token from it
    if (view == null) {
        view = new View(activity);
    }
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

【讨论】:

  • 很可能不是这样 :) 这是命名变量时的拼写错误,如果是这样的话会导致编译时错误。如果 id 不好,则为 NullPointer。
  • 只有在键盘隐藏时才有效@statosdotcom
  • 所以隐藏,改变它,然后再次加注。
【解决方案3】:
firsName.setInputType(InputType.TYPE_CLASS_NUMBER);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    • 2011-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多