启动Activity时自动弹出键盘

99

我有一个比较简单的问题。我有一个带有很多EditText的活动。当我打开活动时,它会自动将焦点聚焦到第一个EditText并显示虚拟键盘。

我该如何防止这种情况发生?


请检查我在这个问题上的答案:http://stackoverflow.com/questions/39593324/cannot-resolve-symbol-showsoftinput/39593871#39593871 - Android Help
当我的应用程序启动并且如果编辑文本视图可用,则键盘会自动显示。如何解决此问题,请单击此处https://www.easycomacademy.com/when-my-app-is-launch-open-and-if-edit-text-view-is-available-so-keyboard-is-automatic-show-enable/ - AhmadUllah Ahmad
20个回答

211

在XML文件的layout标签中使用这些属性:

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

如其他评论成员所述,它不适用于ScrollView,因此您需要将这些属性添加到ScrollView的主子项中。


3
这也解决了我的问题。我将其添加到我的活动的RelativeLayout标签中。谢谢! - Luke
22
有趣的是,当我把上述内容添加到ScrollView时它没有起作用,但是对于LinearLayout来说它是有效的。 - Jerry Brady
1
我认为你必须将它放在最上层布局。 - Utsav Gupta
它对我的顶级滚动视图不起作用,我建议使用Mobile Bloke的答案,到目前为止每次都有效(在Android清单活动android:windowSoftInputMode="stateHidden|adjustResize"中)。 - Arthez
1
需要注意的是,这种解决方案会导致可访问性问题(将不应该被聚焦的视图标记为具有焦点),并可能产生意想不到的后果。 - Renato
显示剩余5条评论

98

你可以将以下内容添加到你的Android清单文件中的Activity标签内:

android:windowSoftInputMode="stateHidden|adjustResize"

这个可以正常工作,但是如果你有一个图片背景并且担心软键盘会缩小这个图片背景,使用 android:windowSoftInputMode="stateHidden|adjustPan" - ArtiomLK

31

我在这里描述了几个实现,但现在我已经将以下属性添加到我的ActivityAndroidManifest.xml中:

android:windowSoftInputMode="stateAlwaysHidden"

我认为即使你使用片段,这仍然是一种简单的方法。

"stateAlwaysHidden" 当活动的主窗口具有输入焦点时,软键盘始终隐藏。


这对我来说在ScrollView和RelativeLayout上都有效。 - iversoncru
对于片段来说,它并不是很好。因为你将“隐藏状态”与包含所有片段的活动相关联,这意味着你想要在活动中显示的任何片段都会受到该属性的影响(而不是按“每个片段”区分)。 - Pelpotronic

11
如果您在活动中有另一个视图,例如 ListView,您也可以这样做:
ListView.requestFocus(); 

在你的onResume()中,可以使用代码editText来获取焦点。

我知道这个问题已经有答案了,但我提供了一个对我有效的替代解决方案 :)


5

在您的Activity代码中使用以下内容:

@Override
public void onCreate(Bundle savedInstanceState) {

getWindow().setSoftInputMode(
   WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

}

这对我没有用 - 当我启动我的活动时,键盘仍然会弹出。 - David Doria
你可以检查这个链接,它会起作用:https://dev59.com/wmMk5IYBdhLWcg3wxAvy#60429616 - Sujeet

5
这几乎是正确的:

https://dev59.com/n2445IYBdhLWcg3w3Nsf#11627976

@Override
public void onCreate(Bundle savedInstanceState) {

getWindow().setSoftInputMode(
   WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

}

但应该是SOFT_INPUT_STATE_HIDDEN而不是SOFT_INPUT_STATE_ALWAYS_VISIBLE


3

我遇到了类似的问题,即使在切换选项卡时,键盘也会自动弹出并保持打开状态,在平板电脑上使用Android 3.2.1。请使用以下方法:

public void setEditTextFocus(EditText searchEditText, boolean isFocused)
{
    searchEditText.setCursorVisible(isFocused);
    searchEditText.setFocusable(isFocused);
    searchEditText.setFocusableInTouchMode(isFocused);
    if (isFocused) {
        searchEditText.requestFocus();
    } else {
        InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
        inputManager.hideSoftInputFromWindow(searchEditText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS );
    }
}   

在每个EditText的活动的onCreate()和onPause()中: setEditTextFocus(myEditText, false); 为每个EditText添加一个OnTouchListener:
    myEditText.setOnTouchListener(new EditText.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            setEditTextFocus(myEditText, true); 
            return false;
        }
    });

针对OnEditorActionListener中的每个EditText

    myEditText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
                            .......
            setEditTextFocus(myEditText, false); 
            return false;
        }
    });

对于layout xml中的每个EditText:

            android:imeOptions="actionDone"
            android:inputType="numberDecimal|numberSigned" // Or something else

可能还有更多的代码优化空间。


3
((InputMethodManager)getActivity().getSystemService("input_method")).hideSoftInputFromWindow(this.edittxt.getWindowToken(), 0);

使用Fragment时,此代码可用于隐藏键盘。当打开一个Fragment时,避免焦点在EditText字段上的键盘。 - Sreelal S

2

我找到了一个简单的解决方案,对我很有用。在您的父布局中设置这些属性:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >

现在,当活动启动时,此主布局将默认获得焦点。

此外,我们可以通过再次将焦点放在主布局上,在运行时从子视图中移除焦点,就像这样:

findViewById(R.id.mainLayout).requestFocus();

希望这对你有用。


1

这是我正在使用的解决方案,虽然不是最好的解决方案,但对我来说效果很好。

 editComment.setFocusableInTouchMode(false);
 editComment.setOnTouchListener(new OnTouchListener(){

                @Override
                public boolean onTouch(View v, MotionEvent event)
                {
                    // TODO Auto-generated method stub
                    editComment.setFocusableInTouchMode(true);
                     editComment.requestFocus() ;
                    return false;
                }});

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