簡體   English   中英

Android - 如何自動滾動 textView 以與 editText 位置對齊?

[英]Android - how to auto scroll textView to align with editText position?

我正在嘗試使用行號創建一個記事本。 我能夠顯示lineNumber但問題是,一旦editext到達屏幕的末尾, textview 就不會自動滾動editext current positionlineNumber

這是 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">

<TextView
    android:id="@+id/lineNumber"
    android:layout_width="40dp"
    android:layout_height="match_parent"
    android:background="#eee"
    android:gravity="center_horizontal"
    android:padding="10dp"
    android:text="1"
    android:textSize="18sp"
    tools:layout_editor_absoluteY="12dp"
    />

<EditText
    android:id="@+id/fileText"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_toEndOf="@+id/lineNumber"
    android:ems="10"
    android:padding="10dp"
    android:gravity="left|top"
    android:inputType="textMultiLine"
    android:scrollbars="vertical"/>
</RelativeLayout>

這是片段代碼

public class FileViewFragment extends Fragment {

TextView lineNumber;
EditText fileText;
String fileName = "";

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_file_viewer, container, false);
    lineNumber = view.findViewById(R.id.lineNumber);
    fileText = view.findViewById(R.id.fileText);

    fileText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            int lines = fileText.getLineCount();
            String linesNumber = "";
            for (int i = 1; i <= lines; i++) {
                linesNumber = linesNumber + i + "\n";
            }
            lineNumber.setText(linesNumber);

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
      return view;
    }
}

那么,如何使用 editext 位置自動滾動 textview?

在設置新 lineNumber Text 后的onTextChanged方法中,您必須計算 EditText 的當前 Y 位置並使用其scrollTo方法將 TextView 滾動到該位置。 EditText Y 位置的計算如下:

int y = fileText.getLayout().getLineTop(fileText.getLineCount()) - (fileText.getHeight()) + (fileText.getPaddingBottom()) + (fileText.getPaddingTop());
lineNumber.scrollTo(0, Math.max(y, 0));

當然,如果您希望 TextView 可滾動,則必須在onCreateView方法中添加以下代碼行:

lineNumber.setMovementMethod(new ScrollingMovementMethod());

要解決滾動問題,您必須更改您的 xml 文件並將 EditText 和 TextView 放在單個父視圖中,並包裝到滾動視圖內的內容,因此現在整個視圖都將是可滾動的。 下面是新的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<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">

    <RelativeLayout
        android:layout_width="40dp"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:background="#eee"/>

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

        <RelativeLayout
            android:id="@+id/containerRl"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/lineNumber"
                android:layout_width="40dp"
                android:layout_height="match_parent"
                android:background="#eee"
                android:gravity="center_horizontal"
                android:padding="10dp"
                android:text="1"
                android:textSize="18sp"
                tools:layout_editor_absoluteY="12dp" />

            <EditText
                android:id="@+id/fileText"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_toEndOf="@+id/lineNumber"
                android:ems="10"
                android:padding="10dp"
                android:gravity="left|top"
                android:inputType="textMultiLine"
                android:scrollbars="vertical"/>
        </RelativeLayout>

    </ScrollView>

</RelativeLayout>

並刪除我之前的答案中的代碼行。

暫無
暫無

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

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