【问题标题】:EditText automatically opens soft keyboard when Fragment is visible with ViewPager当 ViewPager 显示 Fragment 时 EditText 自动打开软键盘
【发布时间】:2012-07-04 21:08:31
【问题描述】:

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

试过了:

android:descendantFocusability="beforeDescendants" 

在片段的主视图上

试过了:

android:windowSoftInputMode="stateHidden"

android:windowSoftInputMode="stateAlwaysHidden"

在活动清单中

试过了:

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

在我的 ViewPager 的 OnPageChangeListener 上

试过了:

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

在我的 Fragment 中的 onCreateView 中

试过了:

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

在我的 Fragment 中的 onStart 中

试过了:

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

在我的 Fragment 中的 onCreateView 中

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

【问题讨论】:

  • 在片段中隐藏键盘尝试: InputMethodManager imm = (InputMethodManager) getSherlockActivity().getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(inputSearch.getWindowToken(), 0); P.S inputSearch 是 EditText 字段
  • 嗨@Mike T,在我的情况下,当软键盘弹出选项卡也移动到键盘顶部时,我正在使用带有viewpager的tablayout以及5个片段页面,我尝试了上面提到的所有内容,但我仍然没有得到答案..请帮助我
  • @Vijaykumar 如果我在下面的回答没有帮助,那么我就没有主意了。对不起

标签: android


【解决方案1】:

This帖子有解决问题的办法。

答案是将android:focusableInTouchMode="true" 添加到包含EditTextLinearLayout。现在它不会自动调出软键盘了。

【讨论】:

  • 非常好,短而干净。谢谢
【解决方案2】:
 <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>

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

【讨论】:

  • 我想要的是如何在显示键盘后将焦点设置在搜索字段上!
【解决方案3】:

你试过了吗?

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

编辑:

试试这个(我现在很糟糕,但试试这个):)

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

【讨论】:

  • android:configChanges 和 android:windowSoftInputMode 的组合很重要。
  • 您的编辑在我第一次滑动到 Fragment 的视图时有效,但之后就无效了。这绝对不是正确的做法
  • 我知道这不是正确的方法..:) 但我也最终得到了这个肮脏的解决方案。在 onPageChangeListener 上尝试我的编辑?它适用于我的情况。
  • 那是我尝试过的地方。它第一次起作用,但在那之后就不行了。
【解决方案4】:

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

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

通过将android:focusableInTouchMode="false" 行添加到您的 EditText 的 xml。

我在这个帖子上从 ACengiz 借了解决方案

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

只有 2 人投票给他?虽然对我来说,在头疼了几个小时后,这是一个节省

【讨论】:

    【解决方案5】:

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

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

    【讨论】:

      【解决方案6】:

      这对我有用。

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

      【讨论】:

        【解决方案7】:

        试试这个

        exitText.setFocusableInTouchMode(true);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-05-22
          • 1970-01-01
          • 1970-01-01
          • 2021-01-24
          相关资源
          最近更新 更多