启动 Activity 时自动弹出键盘

发布于 2024-10-11 11:13:11 字数 100 浏览 5 评论 0原文

我有一个相对简单的问题。我有一个活动,其中有很多 EditText。当我打开活动时,它会自动聚焦到第一个 EditText 并显示虚拟键盘。

我怎样才能防止这种情况发生?

I got a relative simple question. I have an activity with a lot of EditText's in them. When I open the activity it automatically focusses to the first EditText and displays the virtual keyboard.

How can I prevent this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(20

夏末染殇 2024-10-18 11:13:11

在 XML 文件的布局标记中使用此属性:

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

正如其他成员在评论中所报告的,它不适用于 ScrollView,因此您需要将这些属性添加到 ScrollView 的主要子级中代码>.

Use this attributes in your layout tag in XML file:

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

As reported by other members in comments it doesn't works on ScrollView therefore you need to add these attributes to the main child of ScrollView.

情泪▽动烟 2024-10-18 11:13:11

您可以将其添加到您的 Android Manifest 活动中:

android:windowSoftInputMode="stateHidden|adjustResize"

You can add this to your Android Manifest activity:

android:windowSoftInputMode="stateHidden|adjustResize"
小伙你站住 2024-10-18 11:13:11

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

android:windowSoftInputMode="stateAlwaysHidden"

我认为即使您使用 <代码>片段。

stateAlwaysHidden”软键盘始终隐藏
Activity 的主窗口具有输入焦点。

I have several implementations described here, but now i have added into the AndroidManifest.xml for my Activity the property:

android:windowSoftInputMode="stateAlwaysHidden"

I think this is the easy way even if you are using fragments.

"stateAlwaysHidden" The soft keyboard is always hidden when the
activity's main window has input focus.

对风讲故事 2024-10-18 11:13:11

如果您的 Activity 有其他视图(例如 ListView),您还可以:

ListView.requestFocus(); 

在 onResume() 中从 editText 获取焦点。

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

If you have another view on your activity like a ListView, you can also do:

ListView.requestFocus(); 

in your onResume() to grab focus from the editText.

I know this question has been answered but just providing an alternative solution that worked for me :)

城歌 2024-10-18 11:13:11

在您的活动代码中使用它:

@Override
public void onCreate(Bundle savedInstanceState) {

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

}

Use this in your Activity's code:

@Override
public void onCreate(Bundle savedInstanceState) {

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

}
奢望 2024-10-18 11:13:11

https://stackoverflow.com/a/11627976/5217837 这几乎是正确的:

@Override
public void onCreate(Bundle savedInstanceState) {

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

}

但它应该是 SOFT_INPUT_STATE_HIDDEN 而不是 SOFT_INPUT_STATE_ALWAYS_VISIBLE

https://stackoverflow.com/a/11627976/5217837 This is almost correct:

@Override
public void onCreate(Bundle savedInstanceState) {

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

}

But it should be SOFT_INPUT_STATE_HIDDEN rather than SOFT_INPUT_STATE_ALWAYS_VISIBLE

隔岸观火 2024-10-18 11:13:11

我遇到了一个类似的问题,即使在平板电脑上使用 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;
        }
    });

对于每个 EditText< OnEditorActionListener 中的 /code>:

    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

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

I had a simular problem, even when switching tabs the keyboard popped up automatically and stayed up, with Android 3.2.1 on a Tablet. Use the following method:

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

In the onCreate() and in the onPause() of the activity for each EditText:

setEditTextFocus(myEditText, false);

For each EditText an OnTouchListener:

    myEditText.setOnTouchListener(new EditText.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            setEditTextFocus(myEditText, true); 
            return false;
        }
    });

For each EditText in the OnEditorActionListener:

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

And for each EditText in the layout xml:

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

There is probably more code optimizing possible.

梦里的微风 2024-10-18 11:13:11
((InputMethodManager)getActivity().getSystemService("input_method")).hideSoftInputFromWindow(this.edittxt.getWindowToken(), 0);
((InputMethodManager)getActivity().getSystemService("input_method")).hideSoftInputFromWindow(this.edittxt.getWindowToken(), 0);
寂寞花火° 2024-10-18 11:13:11

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

<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();

Hope it will work for you 。

I have found this simple solution that worked for me.Set these attributes in your parent layout:

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

And now, when the activity starts this main layout will get focus by default.

Also, we can remove focus from child views at runtime by giving the focus to the main layout again, like this:

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

Hope it will work for you .

゛时过境迁 2024-10-18 11:13:11

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

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

this is the solution I am using, is not the best solution but it's working well for me

 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;
                }});
浴红衣 2024-10-18 11:13:11

有趣的是,本文档 https://developer.android.com/training/keyboard-input /visibility.html 声明当活动启动并将焦点赋予文本字段时,不会显示软键盘(然后继续向您展示如何显示键盘(如果您想使用一些代码) )。

在我的三星 Galaxy S5 上,这就是我的应用程序(没有清单条目或特定代码)的工作方式 - 没有软键盘。然而,在 Lollipop AVD 上,显示软键盘——这与上面给出的文档相矛盾。

如果您在 AVD 中测试时遇到此行为,您可能需要在真实设备上进行测试,看看会发生什么。

Interestingly, this documentation https://developer.android.com/training/keyboard-input/visibility.html states that when an activity starts and focus is given to a text field, the soft keyboard is not shown (and then goes on to show you how to have the keyboard shown if you want to with some code).

On my Samsung Galaxy S5, this is how my app (with no manifest entry or specific code) works -- no soft keyboard. However on a Lollipop AVD, a soft keyboard is shown -- contravening the doc given above.

If you get this behavior when testing in an AVD, you might want to test on a real device to see what happens.

拿命拼未来 2024-10-18 11:13:11

在以下帖子中有一些很好的答案:阻止 EditText 在 Activity 启动时获得焦点。我经常使用的是 Morgan 编写的以下代码:

<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
    android:focusable="true" 
    android:focusableInTouchMode="true"
    android:layout_width="0px" 
    android:layout_height="0px"/>

<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->
<AutoCompleteTextView android:id="@+id/autotext"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:nextFocusUp="@id/autotext"     
    android:nextFocusLeft="@id/autotext"/>

注意: 虚拟项目必须放置在可聚焦元素之前。

我认为即使与 ScrollView 一起使用它也应该完美地工作,并且在可访问性方面也没有任何问题。

This has some good answers at the following post : Stop EditText from gaining focus at Activity startup. The one I regularly use is the following code by Morgan :

<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
    android:focusable="true" 
    android:focusableInTouchMode="true"
    android:layout_width="0px" 
    android:layout_height="0px"/>

<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->
<AutoCompleteTextView android:id="@+id/autotext"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:nextFocusUp="@id/autotext"     
    android:nextFocusLeft="@id/autotext"/>

NOTE : The dummy item has to be PLACED RIGHT BEFORE the focusable element.

And I think it should work perfectly even with ScrollView and haven't had any problems with accessibility either for this.

东北女汉子 2024-10-18 11:13:11

当您的 EditText 在活动开始时自动获得焦点时,就会发生这种情况。因此,解决此问题的一种简单而稳定的方法是将初始焦点设置到任何其他视图,例如按钮等。

您可以在布局 XML 中执行此操作,无需任何代码。

This occurs when your EditText automatically gets Focus as when you activity starts. So one easy and stable way to fix this, is simply to set the initial focus to any other view, such as a Button etc.

You can do this in your layout XML, no code required..

情域 2024-10-18 11:13:11

接受的答案对我不起作用,这就是为什么给出答案工作解决方案,可能会有帮助!

EditText edt = (EditText) findViewById(R.id.edt);
edt.requestFocus();    
edt.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , 0, 0, 0));
edt.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));

现在键盘已打开,请享受:)

Accepted answer is not working for me, that's why give answer working solution, may be it is helpful !

EditText edt = (EditText) findViewById(R.id.edt);
edt.requestFocus();    
edt.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , 0, 0, 0));
edt.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));

Now keyboard is open enjoy :)

长途伴 2024-10-18 11:13:11

android:windowSoftInputMode="stateHidden|调整Resize"

工作正常

android:windowSoftInputMode="stateHidden|adjustResize"

Working fine

各空 2024-10-18 11:13:11

将以下代码添加到活动 XML 的顶部,并确保视图位于 EditText 之上

<View 
android:layout_width="0dp"
android:layout_height="0dp"
android:focusableInTouchMode="true"/>

Add below code to your top of the activity XML and make sure the View is above EditText

<View 
android:layout_width="0dp"
android:layout_height="0dp"
android:focusableInTouchMode="true"/>
沧桑㈠ 2024-10-18 11:13:11

android:focusableInTouchMode="true"

将以上行添加到具有焦点并导致 softInputKeyboard< 的 EditTextTextInputLayout 的 xml 中/code> 弹出。

这为我们解决了问题,现在键盘不会弹出

android:focusableInTouchMode="true"

Add the above line to xml of EditText or TextInputLayout which has focus and is causing the softInputKeyboard to pop up.

This solved the problem for us and now the keyboard doesn't popup

十年不长 2024-10-18 11:13:11

search_edit_text = (EditText) findViewById(R.id.search_edit_text);

    search_edit_text.requestFocus();
    search_edit_text.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , 0, 0, 0));
    search_edit_text.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));

这对我有用,片段可能有一些不同的语法。 这适用于活动

search_edit_text = (EditText) findViewById(R.id.search_edit_text);

    search_edit_text.requestFocus();
    search_edit_text.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , 0, 0, 0));
    search_edit_text.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));

This works for me guys fragment may have some different syntax . THIS WORKS FOR ACTIVITY

他夏了夏天 2024-10-18 11:13:11

在您的活动代码中使用它:

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

Use this in your Activity's code:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
你的他你的她 2024-10-18 11:13:11

如果您的视图有 EditText 和 Listview,则默认情况下将打开键盘。
要隐藏默认情况下弹出的键盘,请执行以下操作:

this.listView.requestFocus();

确保在获取 editText 视图后请求将焦点放在列表视图上。

例如

this.listView = (ListView) this.findViewById(R.id.list);
this.editTextSearch = (EditText) this.findViewById(R.id.editTextSearch);
this.listView.requestFocus();

,如果您这样做,则 editText 将获得焦点并且键盘将弹出。

If your view has EditText and Listview then Keyboard will open up by default.
To hide keyboard from popping up by default do the following

this.listView.requestFocus();

Make sure you are requesting focus on listview after getting view for editText.

For e.g.

this.listView = (ListView) this.findViewById(R.id.list);
this.editTextSearch = (EditText) this.findViewById(R.id.editTextSearch);
this.listView.requestFocus();

If you do it, then editText will get focus and keyboard will pop up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文