簡體   English   中英

如何配對兩個EditTexts的高度-Android

[英]How to pair the height of two EditTexts - Android

我的應用程序中並排有兩個EditTexts

他們只有1行輸入的樣子

在此處輸入圖片說明

當前,當我在一個EditText鍵入多行輸入時,它會增長,但另一行保持相同的高度-如此處所示

在此處輸入圖片說明

我希望EditTexts高度匹配。 當一個長到另一個時,它的高度就是高度-如此處所示

在此處輸入圖片說明

不確定如何實現此效果。 我不想用其他行填充其他EditText 我想要實際的View增長。

這是我目前的布局

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginTop="10dp"
    android:weightSum="6"
    android:orientation="horizontal">
    <EditText
        android:id="@+id/left"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:inputType="textMultiLine"
        android:gravity="top|start"/>
    <EditText
        android:id="@+id/right"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:inputType="textMultiLine"
        android:gravity="top|start"/>
</LinearLayout>

嘗試在onTextChange方法中更改其他edittext的高度...示例

 yourEditText.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) {
                //Change your other layout height here 

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

請嘗試以下方法,

<LinearLayout
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:orientation="horizontal"
android:weightSum="2">

<LinearLayout
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:orientation="vertical"
    android:padding="5dp">

    <EditText
        android:gravity="top|start"
        android:id="@+id/left"
        android:inputType="textMultiLine"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:layout_width="match_parent" />
</LinearLayout>

<LinearLayout
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:orientation="vertical"
    android:padding="5dp">

    <EditText
        android:gravity="top|start"
        android:id="@+id/right"
        android:inputType="textMultiLine"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:layout_width="match_parent" />
</LinearLayout>

</LinearLayout>

我做了一些研究,發現了這個答案。 基於鏈接的答案,我創建了以下解決方案:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginTop="10dp"
    android:orientation="horizontal"
    android:weightSum="6">

    <View
        android:id="@+id/strut"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerHorizontal="true" />

    <EditText
        android:id="@+id/left"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@id/strut"
        android:gravity="top|start"
        android:layout_alignBottom="@+id/right"
        android:inputType="textMultiLine" />

    <EditText
        android:id="@+id/right"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/strut"
        android:layout_alignParentRight="true"
        android:gravity="top|start"
        android:inputType="textMultiLine" />
</RelativeLayout>

在您的Java代碼中執行以下操作:

        editTextLeft = (EditText) findViewById(R.id.left);
        editTextRight = (EditText) findViewById(R.id.right);
        editTextLeft.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    RelativeLayout.LayoutParams leftLayoutParams = (RelativeLayout.LayoutParams) editTextLeft.getLayoutParams();
                    RelativeLayout.LayoutParams rightLayoutParams = (RelativeLayout.LayoutParams) editTextRight.getLayoutParams();
                    leftLayoutParams.removeRule(RelativeLayout.ALIGN_BOTTOM);
                    rightLayoutParams.addRule(RelativeLayout.ALIGN_BOTTOM,editTextLeft.getId());
                    int lines = editTextRight.getLineCount() > editTextLeft.getLineCount() ? editTextRight.getLineCount() : editTextLeft.getLineCount();
                    editTextLeft.setMinLines(lines);
                    editTextRight.setMinLines(lines);
                    editTextLeft.setLayoutParams(leftLayoutParams);
                    editTextRight.setLayoutParams(rightLayoutParams);

                }
            }
        });
        editTextRight.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if(b) {
                    RelativeLayout.LayoutParams leftLayoutParams = (RelativeLayout.LayoutParams) editTextLeft.getLayoutParams();
                    RelativeLayout.LayoutParams rightLayoutParams = (RelativeLayout.LayoutParams) editTextRight.getLayoutParams();
                    rightLayoutParams.removeRule(RelativeLayout.ALIGN_BOTTOM);
                    leftLayoutParams.addRule(RelativeLayout.ALIGN_BOTTOM, editTextRight.getId());
                    int lines = editTextRight.getLineCount() > editTextLeft.getLineCount() ? editTextRight.getLineCount() : editTextLeft.getLineCount();
                    editTextLeft.setMinLines(lines);
                    editTextRight.setMinLines(lines);
                    editTextLeft.setLayoutParams(leftLayoutParams);
                    editTextRight.setLayoutParams(rightLayoutParams);
                }
            }
        });

暫無
暫無

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

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