【问题标题】:Android Edit Text Covered By Keyboard on Second TapAndroid 在第二次点击时编辑键盘覆盖的文本
【发布时间】:2018-01-06 12:18:27
【问题描述】:

所以我有一个第一次没有被键盘覆盖的 EditText。然后,当您关闭键盘并再次点击编辑文本时,它会覆盖编辑文本。

我花了几个小时研究这个问题并得出结论,它与编辑文本的这两个属性有关。

android:inputType="number"
android:gravity="center"

如果我删除其中任何一个,adjustPan(如我的清单中所示)一直按承诺工作。好像是安卓的bug。但我的编辑文本中需要这两行。解决此问题的最佳方法是什么?

这里是一个稍微精简的 xml 版本:

<RelativeLayout 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"
android:background="@color/colorPrimary"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true">


<LinearLayout
    android:id="@+id/buttons_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal"
    android:weightSum="5">

    <android.support.v7.widget.AppCompatButton
        android:id="@+id/generate_button"
        android:layout_width="match_parent"
        android:layout_height="@dimen/button_height"
        android:layout_marginBottom="@dimen/button_margin"
        android:layout_marginEnd="0dp"
        android:layout_marginLeft="@dimen/button_margin"
        android:layout_marginRight="0dp"
        android:layout_marginStart="@dimen/button_margin"
        android:layout_marginTop="@dimen/button_margin"
        android:layout_weight="1"
        android:background="@color/buttonColor"
        android:elevation="@dimen/button_elevation"
        android:foreground="?attr/selectableItemBackground"
        android:text="@string/generate"
        android:textColor="@color/colorPrimary"
        android:textSize="@dimen/generate_button_title_size"
        android:textStyle="bold" />

    <android.support.v7.widget.AppCompatImageButton
        android:id="@+id/copy_button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="@dimen/button_margin"
        android:layout_weight="4"
        android:adjustViewBounds="true"
        android:background="@color/buttonColor"
        android:elevation="@dimen/button_elevation"
        android:foreground="?attr/selectableItemBackground"
        android:padding="@dimen/button_margin"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_copy"
        android:tint="@color/colorPrimary" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/buttons_layout"
    android:orientation="vertical"
    android:weightSum="10">

    <GridLayout
        android:id="@+id/grid_layout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:layout_weight="6"
        android:animateLayoutChanges="true">
    </GridLayout>


    <TextView
        android:id="@+id/total_text_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:gravity="center"
        android:text="11"
        android:textSize="@dimen/toolbar_title_size"
        android:textStyle="bold" />

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:layout_weight="2"
        android:textColorHint="@color/textColorHint">

        <EditText
            android:id="@+id/num_rolls_edit_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:hint="@string/number_of_rolls"
            android:inputType="number"
            android:maxLength="@integer/edit_text_max_length"
            android:maxLines="1"
            android:text="4"
            android:textColor="@color/textColor"
            android:textSize="@dimen/edit_text_text_size"/>

    </android.support.design.widget.TextInputLayout>

</LinearLayout>

这就是它每次的样子......

这是当前第二次点击时的样子(键盘覆盖编辑文本)...

编辑:我发现键盘在使用 Android 7.0 时可以正常工作。我认为它不适用于低于此的任何东西。这是最近修复的错误还是什么?

此外,我在清单的应用程序和活动部分都包含了 android:windowSoftInputMode="adjustPan|stateHidden" ,但这似乎无法解决问题。

【问题讨论】:

  • 你能发布xml吗?
  • @gunessun XML 发布

标签: java android android-edittext keyboard


【解决方案1】:

将此添加到您的清单中以进行活动。效果很好

 <activity android:name=".Views.UI.MainActivity"
        android:windowSoftInputMode="stateHidden|adjustResize">

【讨论】:

    【解决方案2】:

    按照document 的建议,尝试将清单文件中的活动配置的adjustPan 更改为adjustResize

    这(adjustPan)通常不如调整大小,因为用户可能需要关闭软键盘才能到达窗口的模糊部分并与之交互。

    编辑

    如果您不希望在打开键盘时视图被压缩/调整,请将ScrollView 添加到您的根RelativeLayout。这样当adjustResize 将相应地滚动视图而不是“粉碎”它们。

    希望这会有所帮助!

    【讨论】:

    • 我之前尝试过,它只会破坏视图,尤其是编辑文本,所以你几乎看不到它。我坚持使用adjustPan
    • 但是添加滚动视图会破坏需要相对布局的内部线性布局。 Scrollview 禁用 android:layout_alignParentBottom="true"
    • 尝试在ScrollView 标签中添加android:fillViewport="true",并确保您的ScrollView 具有match_parent 作为高度。 @贾里德
    【解决方案3】:

    如果你这样做了,我认为你没有在 XML 中使用滚动视图,而不是你需要制作一些代码 android mainfeast。 <activity android:name=".activity.ActivityProductDetail" android:windowSoftInputMode="stateAlwaysHidden|adjustResize" />

    【讨论】:

    • 我已经尝试过滚动视图、adjustResize,但它们都不能正常工作。
    【解决方案4】:

    在您的第一个屏幕截图中,文本“Dice”被截断,在第二个屏幕截图中,editText 被覆盖,显示“Dice”,考虑删除标题 editText“Dice”?

    我还看到你使用了 scrollView 并说你可以使用 AlignParentBottom,你能用 layoutBelow 替换它吗?

    【讨论】:

    • 你的标题栏有没有可能是“骰子”?
    【解决方案5】:

    RelativeLayoutalignParentBottom 和几个视图总是有问题的,尤其是键盘。我建议使用scrollview 并在LinearLayout 中添加视图,按钮将位于最后,但不一定位于视图的底部。

    那么你甚至不需要使用adjustPan。比如:

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
        <!-- grid view + textview + textEdit + your buttons at the end -->
    
    </LinearLayout>
    

    【讨论】:

      【解决方案6】:

      您可以将布局添加到 ScrollView 并监听 onGloablLayout() 回调。如果 rootview 和您的视图的高度差异 >100 像素,则可能是键盘,您可以滚动到您的 EditText,这样它就不会被键盘阻止。

      final View layout = findViewById(R.id.layoutparentID);
          layout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
              @Override
              public void onGlobalLayout() {
                  int heightDiff = layout.getRootView().getHeight() - layout.getHeight();
                  if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
      
                      final Runnable r = new Runnable() {
                          public void run() {
                              scrollView.smoothScrollTo(0, editText.getBottom());
                          }
                      };
                      activityRootView.getHandler().postDelayed(r, 100);
                  }
              }
          });
      

      【讨论】:

        【解决方案7】:

        检查此代码,隐藏软键盘

         try {
                View view = this.getCurrentFocus();
                if (view != null) {
                    InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-11-22
          • 2017-09-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-07-01
          • 2015-01-30
          • 1970-01-01
          相关资源
          最近更新 更多