根据文本量更改 textedit 的高度

Change height for textedit depending on amount of text

所以我正在尝试构建聊天和您编写消息的文本编辑器。我想根据文本量更改高度。

还有一件好事要知道,这是一个片段。

就像在 Messenger 或普通消息传递应用程序中一样。

科特林:

fun initTextWatcher(){
        var tw: TextWatcher = object : TextWatcher {
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
                val textview = viewOfLayout.chat_input_text
                if (activity != null) {
                    activity?.runOnUiThread {
                        textview.height = 100
                    }
                }
            }
            override fun afterTextChanged(s: Editable) {}
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
        }

        viewOfLayout.chat_input_text.addTextChangedListener(tw)
    }

XML:

 <android.support.constraint.ConstraintLayout
            android:layout_width="0dp" android:layout_height="wrap_content"
            android:background="@color/White"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:id="@+id/chat_input_text_Send_container">

        <Button
                android:text="@string/Send"
                android:layout_width="wrap_content"
                android:layout_height="35dp"
                android:id="@+id/chat_send_button" app:layout_constraintEnd_toEndOf="parent"
                android:layout_marginEnd="10dp"
                android:layout_marginBottom="10dp" app:layout_constraintBottom_toBottomOf="parent"
                android:background="@drawable/chat_round_corners" android:textColor="@color/White"
                android:textSize="15sp"/>
        <EditText
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:maxHeight="70dp"
                android:minHeight="35dp"
                android:ems="10"
                android:inputType="textCapSentences|textMultiLine"
                android:id="@+id/chat_input_text" android:layout_marginTop="10dp"
                app:layout_constraintTop_toTopOf="parent" android:layout_marginBottom="10dp"
                app:layout_constraintBottom_toBottomOf="parent" android:layout_marginEnd="8dp"
                app:layout_constraintEnd_toStartOf="@+id/chat_send_button" android:layout_marginStart="8dp"
                app:layout_constraintStart_toStartOf="parent" android:textColor="@color/colorPrimary"
                android:gravity="top|left" android:textSize="15sp"
                android:background="@drawable/chat_text_round_corners" android:padding="4dp"/>
        <ProgressBar
                style="?android:attr/progressBarStyle"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:indeterminateTint="@color/White"
                android:id="@+id/chat_progressbar_send"
                app:layout_constraintTop_toTopOf="@+id/chat_send_button"
                app:layout_constraintEnd_toEndOf="@+id/chat_send_button"
                app:layout_constraintStart_toStartOf="@+id/chat_send_button"
                app:layout_constraintBottom_toBottomOf="@+id/chat_send_button" android:visibility="invisible"/>
    </android.support.constraint.ConstraintLayout>

每次文本更改为 editText 的参数时,我都会通过设置布局参数来修复它。

fun initTextWatcher(){
        var tw: TextWatcher = object : TextWatcher {
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
                val chat_textedit = viewOfLayout.chat_input_text
                if (activity != null) {
                    activity?.runOnUiThread {
                        val lparams = chat_textedit.layoutParams as ConstraintLayout.LayoutParams
                        chat_textedit.layoutParams = lparams
                    }
            }
        }
        override fun afterTextChanged(s: Editable) {}
        override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
    }
    viewOfLayout.chat_input_text.addTextChangedListener(tw)
}