【问题标题】:EditText In android goes back to the top the layout when typing textandroid中的EditText在输入文本时回到顶部布局
【发布时间】:2021-09-20 00:26:31
【问题描述】:

当在相对布局(包含在滚动视图中)中添加 EditText 视图时,在输入文本时它会将用户带回到活动的顶部,(捕捉到屏幕顶部)使用户无法看到他们的内容正在打字。

复制问题

这是该问题的最小再现:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity"
    xmlns:android="http://schemas.android.com/apk/res/android">

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

        <ImageView
            android:layout_width="100dp"
            android:layout_height="1000dp"
            android:src="@color/purple_500"/>
        <EditText
            android:layout_width="400dp"
            android:layout_height="100dp"
            android:translationY="800dp"/>

    </RelativeLayout>

</ScrollView>

到目前为止我所尝试的

到目前为止,我已经尝试将 &lt;activity android:windowSoftInputMode="stateVisible|adjustResize" .&gt; 添加到 android 清单中,但它没有任何区别并填充 EditText 尽管我找不到其他发生这种情况的情况。

wrap_content 添加到android:layout_height="", 并没有解决问题

编辑

我需要在线性布局中包含编辑文本,在相对布局中,如此处所示 -https://stackoverflow.com/questions/40316454/edittext-moves-textview-out-of-the-screen-什么时候打开键盘

【问题讨论】:

  • 这可能是因为你的 RecyclerView 有 match_parent 而不是 wrap_content。试试看
  • 我可以知道你为什么将 ImageView 的高度设置为 1000dp 吗?
  • 这只是证明当输入@vijay 时 EditText 不在屏幕上
  • 您是否考虑过使用 ConstraintLayout 而不是 RelativeLayout(已弃用)?

标签: java android xml android-layout android-edittext


【解决方案1】:

为避免在 EditText 上写入期间自动滚动到顶部效果,您可以创建 ScrollView 的子类并将 0 返回到受保护的方法:computeScrollDeltaToGetChildRectOnScreen(Rect rect)

computeScrollDeltaToGetChildRectOnScreen

计算在 Y 方向上滚动的量以获得 矩形完全在屏幕上(或者,如果比屏幕高,在 至少是它的第一个屏幕大小块)。

创建 ScrollView 的子类并在该方法上返回 0,如下所示:

public class CustomScrollView extends ScrollView {

    public CustomScrollView(Context context) {
        super(context);
    }

    public CustomScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected int computeScrollDeltaToGetChildRectOnScreen(Rect rect) {
        return 0;
    }
}

Xml 用法:

<?xml version="1.0" encoding="utf-8"?>
<my.package.name.CustomScrollView
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity"
    xmlns:android="http://schemas.android.com/apk/res/android">

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

        <ImageView
            android:layout_width="100dp"
            android:layout_height="1000dp"
            android:src="@color/purple_500"/>
        <EditText
            android:layout_width="400dp"
            android:layout_height="100dp"
            android:translationY="800dp"/>

    </RelativeLayout>

</my.package.name.CustomScrollView>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-04
    • 2016-08-29
    • 2014-01-24
    • 2011-05-15
    • 2020-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多