簡體   English   中英

EditText在android中輸入文本時回到頂部的布局

[英]EditText In android goes back to the top the layout when typing text

在輸入文本時在相對布局(包含在滾動視圖中)中添加 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>

到目前為止我嘗試過的

到目前為止,我已經嘗試將<activity android:windowSoftInputMode="stateVisible|adjustResize" .>到 android 清單,但它沒有任何區別並填充 EditText 盡管我找不到其他發生這種情況的情況。

wrap_content添加到android:layout_height="",這還沒有解決這個問題

編輯

我需要在相對布局中包含線性布局中的編輯文本,如下所示 - https://stackoverflow.com/questions/40316454/edittext-moves-textview-out-of-the-screen-when-they -鍵盤正在打開

為了避免在 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM