Android 当使用 ViewPager 可以看到 Fragment 时,EditText 会自动打开软键盘

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11327785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:59:51  来源:igfitidea点击:

EditText automatically opens soft keyboard when Fragment is visible with ViewPager

android

提问by Mike T

I have a Fragment(the compatibility version) with an EditTextin its layout. I'm using a ViewFlipperto flip between fragments. When I get to this particular Fragment, the soft keyboard opens up automatically. This is not what I want. Here is what I've tried to stop it or hide it.

我有一个Fragment(兼容版本)的EditText布局。我正在使用 aViewFlipper在片段之间翻转。当我到达这个特定的时候Fragment,软键盘会自动打开。这不是我想要的。这是我试图阻止或隐藏它的方法。

Tried:

尝试:

android:descendantFocusability="beforeDescendants" 

on the fragment's main view

在片段的主视图上

Tried:

尝试:

android:windowSoftInputMode="stateHidden"

and

android:windowSoftInputMode="stateAlwaysHidden"

in the manifest for the activity

在活动的清单中

Tried:

尝试:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mViewPager.getChildAt(position).getWindowToken(), 0);

on the OnPageChangeListener of my ViewPager

在我的 ViewPager 的 OnPageChangeListener 上

Tried:

尝试:

InputMethodManager imm = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(voucherView.findViewById(R.id.redeem_mobile_number).getWindowToken(), 0);

in onCreateView in my Fragment

在我的 Fragment 中的 onCreateView

Tried:

尝试:

InputMethodManager imm = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getView().findViewById(R.id.redeem_mobile_number).getWindowToken(), 0);

in onStart in my Fragment

在我的片段中的 onStart

Tried:

尝试:

voucherView.findViewById(R.id.redeem_mobile_number).clearFocus();

in onCreateView in my Fragment

在我的 Fragment 中的 onCreateView

It seems to me like onPageChangeListener is the place to do this because the other calls happen before the soft keyboard is actually open. Any help would be great.

在我看来, onPageChangeListener 是这样做的地方,因为其他调用发生在软键盘实际打开之前。任何帮助都会很棒。

回答by Mike T

Thispost has a solution to the problem.

这个帖子有一个解决问题的方法。

The answer was to add android:focusableInTouchMode="true"to the LinearLayoutcontaining the EditText. Now it doesn't bring up the soft keyboard automatically.

答案是添加android:focusableInTouchMode="true"LinearLayout包含EditText. 现在它不会自动调出软键盘。

回答by Yajneshwar Mandal

 <LinearLayout 
         android:id="@+id/layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:focusable="true" 
        android:focusableInTouchMode="true"         
        >

    <EditText
        android:id="@+id/retailer_search_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

</LinearLayout>

Just follow it . And EditText should be within a LinearLayout.

只要跟着它。并且 EditText 应该在 LinearLayout 内。

回答by Mohsin Naeem

Have you tried this?

你试过这个吗?

<activity
    android:name=".YourActivityName"
    android:configChanges="keyboardHidden|orientation"
    android:windowSoftInputMode="stateHidden" />

EDIT:

编辑:

try this (I now it is a bad one but give a try to this) :)

试试这个(我现在这是一个坏的,但试试这个):)

    Thread splashTread = new Thread() {
        @Override
        public void run() {
            try {

                sleep(1);
            } catch (InterruptedException e) {
                // do nothing
            } finally {
                runOnUiThread(new Runnable() {
                    public void run() {
                        InputMethodManager imm = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(youreditText.getWindowToken(), 0);

                    }
                });

            }
        }
    };
    splashTread.start();

回答by Rahul

Add this to your activity tag in AndroidManifest.xmlkeyboardHidden: will not let it open soft input keyboard automatically.

将此添加到您的活动标签中AndroidManifest.xmlkeyboardHidden: 不会让它自动打开软输入键盘。

android:configChanges="orientation|keyboardHidden|screenSize"
android:windowSoftInputMode="stateAlwaysHidden|adjustResize|stateHidden"

回答by miberg81

I had a soft keyboard disturbingly popup and pushing all views up when I click on an edit text in a fragment view (my app is a app of nested fragments - fragment in fragment)

当我单击片段视图中的编辑文本时,我有一个令人不安的软键盘弹出并向上推所有视图(我的应用程序是嵌套片段的应用程序 - 片段中的片段)

I tried a dozen solutions here and on the internet, but nothing helped except this, which is edit the EditTextitself in XML (not the view above/not the manifest/not overwride on create activities/not any other)

我在这里和互联网上尝试了十几种解决方案,但除此之外没有任何帮助,即EditText在 XML 中编辑自身(不是上面的视图/不是清单/不覆盖创建活动/不是任何其他)

by adding android:focusableInTouchMode="false"line to your EditText's xml.

通过android:focusableInTouchMode="false"在 EditText 的 xml 中添加一行。

I borrowed the solution from ACengiz on this thread

我在这个线程上从 ACengiz 借用了解决方案

how to block virtual keyboard while clicking on edittext in android?

在android中单击edittext时如何阻止虚拟键盘?

Only 2 people voted for him? although for me it was a saver after hours of a headache

只有2个人投票给他?虽然对我来说这是一个经过数小时头痛的救星

回答by ASP

This worked for me.

这对我有用。

edittext.setInputType(InputType.TYPE_NULL);      
if (android.os.Build.VERSION.SDK_INT >= 11)   
{  
    edittext.setRawInputType(InputType.TYPE_CLASS_TEXT);  
    edittext.setTextIsSelectable(true);  
}

回答by Shreyas K C

Try This

尝试这个

exitText.setFocusableInTouchMode(true);