简体   繁体   English

Android:滚动视图内的 EditText:阻止软键盘自动显示

[英]Android: EditText inside Scrollview: block softkeyboard autoshow

When an Activity has an EditText inside a scrollview, the softkeyboard is shown automatically.当活动在滚动视图中有 EditText 时,软键盘会自动显示。

But the EditText is not the main feature of the Activity.但是 EditText 不是 Activity 的主要功能。

How to prevent the SoftKeyboard from showing automatically when the Activity is created?创建Activity时如何防止SoftKeyboard自动显示?

Some things dont solve the problem:有些事情不能解决问题:

  • Adding code to hide the softkeyboard causes problems reshowing it:添加代码来隐藏软键盘会导致重新显示它的问题:

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

  • or in the manifest: android:windowSoftInputMode="stateAlwaysHidden"或在清单中: android:windowSoftInputMode="stateAlwaysHidden"

You simply have to remove EditText focus.您只需删除EditText焦点。

1-add android:windowSoftInputMode="stateHidden" to the activity tag in the manifest 1-将android:windowSoftInputMode="stateHidden"添加到清单中的活动标签

or或者

2-use this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 2-使用this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

or或者

3-use edittext.clearFocus(); 3-使用edittext.clearFocus();

or或者

4- set 4-套

    android:focusable="true"
    android:focusableInTouchMode="true" 

for another view.另一种看法。

Note: edittext.clearFocus();注意: edittext.clearFocus(); may not work because it sets the focus back to another focusable view in the activity, so with one view it just reset focus-ability to the same view.可能不起作用,因为它将焦点设置回活动中的另一个可聚焦视图,因此对于一个视图,它只是将焦点能力重置为同一视图。

you can use edittext.clearFocus();你可以使用edittext.clearFocus(); or add android:windowSoftInputMode="stateHidden"或添加android:windowSoftInputMode="stateHidden"

您可以在 onCreate 方法中使用此代码...

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

The correct way to block the softkeyboard from showing automatically is to prevent it from focusing on the EditText.阻止软键盘自动显示的正确方法是防止它专注于 EditText。

For each edittext in your layout:对于布局中的每个编辑文本:

onCreate()
    editText = (EditText)findViewById(R.id.edittext);
    editText.setFocusable(false);

In your xml layout file:在您的 xml 布局文件中:

<EditText
    ...
    android:onClick="enableFocusable"

Then in the java class, a method...然后在java类中,一个方法...

public void enableFocusable(View view) {
    editText.setFocusableInTouchMode(true);
    editText.requestFocus();
    if (view.requestFocus()) {
        InputMethodManager imm = (InputMethodManager)
                getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
    }
}

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

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