根据文本量更改textedit的高度

时间:2019-03-25 09:55:11

标签: java android xml kotlin android-textinputedittext

因此,我正在尝试在您编写消息的地方建立聊天和textEdit。我想根据文本量更改高度。

还知道这是一个片段。

就像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)
}

1 个答案:

答案 0 :(得分:0)

已解决:

每次文本更改为 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)
}