第二次点击时,Android的编辑文本被键盘覆盖

7

我有一个EditText,在第一次使用时不会被键盘遮挡。但是当你关闭键盘并再次点击EditText时,它会覆盖掉EditText。

我已经花了数小时研究这个问题,并得出结论它与EditText的这两个属性有关:

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

如果我删除其中任何一个,即在我的清单中添加的adjustPan,它会始终按照承诺工作。似乎是Android的一个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>

每次都应该是这个样子... enter image description here

目前第二次点击时的效果如下(键盘覆盖了EditText)... enter image description here

编辑: 我发现在使用Android 7.0时,键盘可以正常工作。我认为在此之下的版本上键盘无法正常工作。这是最近修复的漏洞还是其他原因导致的呢?

此外,我已经在我的清单文件中的应用程序和活动部分中包含了android:windowSoftInputMode="adjustPan|stateHidden",但似乎并没有解决这个问题。


你能发布XML吗? - gunesevitan
@gunessun 发布了 XML - Jared
7个回答

5

在你的活动清单中添加这个。它可以正常工作。

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

4
尝试在清单文件中更改活动配置中的adjustPanadjustResize,因为文档建议使用后者。这通常比调整大小更不可取,因为用户可能需要关闭软键盘才能访问和交互窗口的遮挡部分。 编辑 如果您不希望在打开键盘时视图被压缩/调整,请将一个ScrollView添加到您的根RelativeLayout中。这样,当使用adjustResize时,它会相应地滚动视图而不是“压缩”它们。
希望这有所帮助!

我以前尝试过那种方式,但它会破坏视图,特别是编辑文本,以至于你几乎看不到它。我还是坚持使用adjustPan。 - Jared
但是添加滚动视图会破坏需要相对布局的内部线性布局。ScrollView 禁用了 android:layout_alignParentBottom="true"。 - Jared
1
尝试在“ScrollView”标记中添加“android: fillViewport =”true“”,并确保您的“ScrollView”高度为“match_parent”。@Jared - Jiyeh

2

我认为您在XML中没有使用滚动视图,如果您使用了它,那么您需要在AndroidManifest文件中添加一些代码。

<activity android:name=".activity.ActivityProductDetail" android:windowSoftInputMode="stateAlwaysHidden|adjustResize" />

1
我已经尝试过scrollviews、adjustResize,但它们都不能正常工作。 - Jared

2
在您的第一个截图中,“Dice”文本被截断,在第二个截图中,当editText被覆盖时,“Dice”显示出来了。是否考虑移除标题editText“Dice”?
另外我看到您使用了scrollView,并且说可以使用AlignParentBottom,您能否将其替换为layoutBelow

或者“Dice”是您的标题栏? - AnthonyK

1
使用alignParentBottom和多个视图的RelativeLayout总是有问题,特别是在使用键盘时。我建议使用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>


1
你可以将布局添加到 ScrollView 中,并监听 onGloablLayout() 回调。如果根视图和你的视图高度差异大于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);
            }
        }
    });

0

请检查此代码,隐藏软键盘

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接