【问题标题】:Keyboard hiding EditTexts in Fragments键盘在片段中隐藏 EditText
【发布时间】:2015-09-29 22:40:16
【问题描述】:

编辑: 我需要使用键盘,但它隐藏了我的EditText,我需要它来滚动以便键盘不会隐藏它。

我使用的是三星平板电脑。

我的风格:

parent="android:Theme.Holo.NoActionBar.Fullscreen"

EditText 字段位于可滚动视图中,如下所示:

片段布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_height="match_parent"
         android:layout_width="match_parent"
         android:paddingBottom="@dimen/activity_vertical_margin"
         android:paddingLeft="@dimen/activity_horizontal_margin"
         android:paddingRight="@dimen/activity_horizontal_margin"
         android:paddingTop="@dimen/activity_vertical_margin">

<ScrollView
    android:layout_height="wrap_content"
    android:layout_width="match_parent">

    <LinearLayout
        android:layout_height="match_parent"
        android:layout_marginBottom="20dp"
        android:layout_width="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:text="@string/depot_code"/>

        <EditText
            android:hint="@string/enter_depot_code"
            android:id="@+id/etlocationId"
            android:imeOptions="actionNext"
            android:inputType="number"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:lines="1"
            android:maxLength="12"
            android:singleLine="true">

            <requestFocus/>
        </EditText>

        <TextView
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:text="@string/name"/>

        <EditText
            android:hint="@string/enter_name"
            android:id="@+id/etname"
            android:imeOptions="actionNext"
            android:inputType="textPersonName"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:lines="1"
            android:maxLength="24"
            android:singleLine="true"/>

        <TextView
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:text="@string/street"/>

        <EditText
            android:hint="@string/enter_street"
            android:id="@+id/etstreet"
            android:imeOptions="actionNext"
            android:inputType="textPostalAddress"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:lines="1"
            android:maxLength="24"
            android:singleLine="true"/>

        <TextView
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:text="@string/suburb"/>

        <EditText
            android:hint="@string/enter_suburb"
            android:id="@+id/etsuburb"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:lines="1"
            android:maxLength="24"
            android:singleLine="true"/>

        <TextView
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:text="@string/state"/>

        <Spinner
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:id="@+id/spinner"
            android:imeOptions="actionNext"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_width="fill_parent"
            android:spinnerMode="dropdown"/>

        <TextView
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:text="@string/phone"/>

        <EditText
            android:hint="@string/enter_phone"
            android:id="@+id/etphone"
            android:imeOptions="actionDone"
            android:inputType="phone"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:lines="1"
            android:maxLength="12"
            android:singleLine="true"/>

        <Button
            android:id="@+id/btnadd"
            android:layout_gravity="center"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_width="wrap_content"
            android:text="@string/add"/>
    </LinearLayout>
</ScrollView>
</FrameLayout>

Activity 布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/tech_controller"
          android:layout_height="match_parent"
          android:layout_width="match_parent"
          android:orientation="vertical"
          android:paddingBottom="@dimen/activity_vertical_margin"
          android:paddingLeft="@dimen/activity_horizontal_margin"
          android:paddingRight="@dimen/activity_horizontal_margin"
          android:paddingTop="@dimen/activity_vertical_margin">

< .../ Layouts. ../>

<FrameLayout
    android:id="@+id/second"
    android:layout_height="match_parent"
    android:layout_width="fill_parent"/>
</LinearLayout>

有人问过类似的问题,但我没有找到有效的答案。

我有一个项目使用许多附加到一个活动的片段。 在我的活动清单中:

android:windowSoftInputMode="stateHidden"

有些片段需要用户输入。

在我使用的片段布局中:

每个片段嵌套如下:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent" .../>

<ScrollView
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="vertical">

        <EditText
            android:imeOptions="actionNext"
            or
            android:imeOptions="actionDone" .../>

这很好用。除了 键盘会隐藏页面下方的 EditText。

我一遍又一遍地阅读:

http://developer.android.com/reference/android/widget/TextView.html#attr_android:imeOptions

尝试了这些建议,但是当调用 Activity 时键盘是可见的,这是我不想要的。它也不起作用。也许是因为它是一个片段?

SoftKeyboard hiding EditText

Android Keyboard hides EditText

我一直在尝试制定一个动态解决方案。任何帮助表示赞赏。

edit:这是在平板电脑上使用的,只有一个键盘选项,可以更改语言。


编辑:我的解决方案

我已经通过安装另一个软键盘解决了这个问题。 https://code.google.com/p/softkeyboard/wiki/HowTo

【问题讨论】:

  • 可以提供截图吗?
  • 您使用的 IME 有它自己的“浮动”布局,您可以尝试使用 Google 键盘等内置 IME 的代码或将您的 IME 设置为正常的全角布局吗?
  • @Heyyou 长按键盘上的空格键。 thgis 将弹出一个对话框,为您提供一个选择器,您可以在其中选择另一个键盘。
  • @Heyyou 尝试设置 > 语言和输入 > 并寻找键盘
  • 您是否使用getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 标志或任何其他标志来全屏显示您的活动?

标签: android android-fragments samsung-mobile window-soft-input-mode imeoptions


【解决方案1】:

尝试仅将WindowsSoftInputMode 用作adjustpan

例如:

android:windowSoftInputMode="adjustPan" 

【讨论】:

    【解决方案2】:

    尝试在此SO answer 中提到的片段的onActivityCreated() 函数期间调用InputMethodManager.hideSoftInputFromWindow()

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        AppUtil.hideKeyboard(getActivity(), getView());
    }
    

    hideKeyboard() 看起来像这样

    public static void hideKeyboard(Activity activity, View viewToHide) {
        InputMethodManager imm = (InputMethodManager) activity
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(viewToHide.getWindowToken(), 0);
    }
    

    【讨论】:

      【解决方案3】:

      您可以尝试将其放入片段的 onActivityCreated 中:

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

      这不应该在加载或开始您的活动时使用片段。

      【讨论】:

      • 除了我之前的回答之外,尝试在你的android清单中添加这个:android:windowSoftInputMode="adjustResize"我认为它会起作用:)
      • 你也可以试试这个getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
      • 你好@Heyyou,我的直觉可能是滚动视图和软输入键盘之间存在冲突:) 无论如何,希望你能找到解决方案。当我再次找到解决方案时,我会回到这里^_^
      【解决方案4】:

      我已尝试此代码并解决了我的问题。

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

      【讨论】:

        【解决方案5】:

        我通过 google play store 安装了另一个软键盘解决了这个问题。

        Petey's cmets 让我探索了这个解决方案。

        您使用的 IME 有它自己的“浮动”布局,您可以尝试使用 Google 键盘等内置 IME 的代码或将您的 IME 设置为正常的全角布局吗?

        所以我想,无论怎么玩视图布局都不会影响具有独立布局的软键盘。

        我使用了这个链接https://code.google.com/p/softkeyboard/wiki/HowTo

        它甚至适用于 Android Manifest:

        android:windowSoftInputMode="stateHidden"
        

        然后我使用imeoptions 来控制视图中编辑器光标的流动。

        编辑更新

        我刚刚在一个没有品牌的安卓平板电脑上试用了这个应用程序,它带有一个通用的安卓键盘,它可以工作,无需改变任何东西。看来三星默认键盘很痛苦。

        【讨论】:

          【解决方案6】:

          将其更改为 LinearLayout

          <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
          

          <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_height="match_parent"
              android:layout_width="match_parent"
              android:orientation="vertical"
              android:paddingBottom="@dimen/activity_vertical_margin"
              android:paddingLeft="@dimen/activity_horizontal_margin"
              android:paddingRight="@dimen/activity_horizontal_margin"
              android:paddingTop="@dimen/activity_vertical_margin">
          

          (和结束标记)。注意方向。

          更新清单删除此

          <application ... >
              <activity
                android:windowSoftInputMode="stateVisible|adjustResize|adjustPan">
                  ...
              </activity>
              ...
          </application>
          

          改变这个(高度)

          <ScrollView
              android:layout_height="wrap_content"
              android:layout_width="match_parent">
          

          【讨论】:

          • @Heyyou 可以添加完整的 xml。我对父布局感兴趣
          • @Heyyou 我在一个新项目中尝试了你的 XML(使用线性布局),它运行良好。我又做了一个编辑。尝试从 mainifest 中删除它
          【解决方案7】:

          请记住,隐藏键盘不适用于 getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

          【讨论】:

            【解决方案8】:

            在显示您的 Snackbar 小部件之前,像这样隐藏键盘,

            public void showSnackBar(){ 
            InputMethodManager manager = (InputMethodManager)getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
            manager.hideSoftInputFromWindow(getView().getWindowToken(),0); 
            Snackbar snackbar = Snackbar.make(getView(), "Some message!", Snackbar.LENGTH_INDEFINITE).show(); 
            }
            

            【讨论】:

              【解决方案9】:

              对我有用的是在我的 FrameLayout 中以与您相同的方式使用 ScrollView,我在其中添加了以下属性:android:windowSoftInputMode="adjustPan"。最后,它看起来像这样并且它正在工作。我不需要以编程方式更改任何内容:

               <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:tools="http://schemas.android.com/tools"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  tools:context=".ProductFragment"
                  android:windowSoftInputMode="adjustPan">
              
                  <ScrollView
                      android:layout_height="wrap_content"
                      android:layout_width="match_parent">
                    ...
                  </ScrollView>
              </FrameLayout>
              

              【讨论】:

              • android:windowSoftInputMode ?这是绝对错误的。
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2021-12-16
              • 2014-08-07
              • 1970-01-01
              • 1970-01-01
              • 2023-04-02
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多