如何在EditText上隐藏Android软键盘

58
我有一个Activity,里面有一些EditText字段和一些按钮用于方便地填充这些字段。但是当用户触摸其中一个EditText字段时,Android软键盘会自动弹出。我希望默认情况下它保持隐藏,除非用户长按菜单按钮。我已经搜索了几个解决方案,但到目前为止我还无法让它们起作用。

我尝试了以下方法:

1 - 在onCreate方法中,

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

2-同样在onCreate方法中,

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);

在清单文件中,将 3 和 fIn 改为如下形式:

<activity android:name=".activityName" android:windowSoftInputMode="stateAlwaysHidden"/>

这些方法都不起作用。每当用户单击EditText字段时,软键盘就会出现。我只希望在用户通过长按菜单键明确显示它时才出现软键盘。

为什么这不起作用?


尝试使用我的答案。也许它会有所帮助 来自这里 - ch13mob
在Android TV中,您可能需要实现一个编辑文本的行为,其中您需要创建一个不可编辑但可点击的编辑文本。这个链接可能会对您有所帮助- https://dev59.com/tGox5IYBdhLWcg3wDgEz#70285647 - Anoop M Maddasseri
16个回答

1
只需使用以下方法。
private fun hideKeyboard(activity: Activity, editText: EditText) {
    editText.clearFocus()
    (activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).hideSoftInputFromWindow(editText.windowToken, 0)
}

1

我是一名有用的助手,可以为您进行文本翻译。

以下是需要翻译的内容:

基于相同简单指令的三种方法:

a) 定位(1)一样容易得到结果:

android:focusableInTouchMode="true"

在布局中的任何先前元素的配置中,例如:
如果您的整个布局由以下内容组成:
<ImageView>

<EditTextView>

<EditTextView>

<EditTextView>

然后,您可以在ImageView参数中写入(1),这将吸引Android注意ImageView而不是EditText。

b)如果您有另一个先例元素而不是ImageView,则可能需要将(2)添加到(1):

android:focusable="true"

c) 您还可以在视图元素的顶部创建一个空元素:

<LinearLayout
  android:focusable="true"
  android:focusableInTouchMode="true"
  android:layout_width="0px"
  android:layout_height="0px" />

到目前为止,这个方案似乎是我见过的最简单的替代方案。希望它能有所帮助...


0

隐藏键盘

editText.setInputType(InputType.TYPE_NULL);

显示键盘

etData.setInputType(InputType.TYPE_CLASS_TEXT);
etData.setFocusableInTouchMode(true);

在父布局中

android:focusable="false"

0
   public class NonKeyboardEditText extends AppCompatEditText {

    public NonKeyboardEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onCheckIsTextEditor() {
        return false;
    }
}

并添加

NonKeyboardEditText.setTextIsSelectable(true);

1
你好,请考虑添加一些额外的信息来介绍你的代码片段,例如解释它如何解决问题以及为什么这样做。 - Matthias Beaupère

0
weekText = (EditText) layout.findViewById(R.id.weekEditText);
weekText.setInputType(InputType.TYPE_NULL);

-1

我也遇到了同样的问题,我通过这种方法解决了它,

   editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    // do something..
            }
            
                closeKeyborad();
                return true;
            }
            return false;
        }
    });

在返回 true 之前调用该函数。

private void closeKeyborad() {
    View view = this.getCurrentFocus();
    if (view != null){
        InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken() , 0);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接