根据文本量更改文本编辑的高度

因此,我试图建立聊天和文本编辑你写消息的地方。我想根据文本量更改高度。


要知道的一件好事也是,这是在一个片段中。


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


Kotlin:


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"/>



潇潇雨雨
浏览 81回答 1
1回答

慕尼黑8549860

我通过在每次文本更改为的参数时设置布局参数来修复它。editTextfun initTextWatcher(){&nbsp; &nbsp; &nbsp; &nbsp; var tw: TextWatcher = object : TextWatcher {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val chat_textedit = viewOfLayout.chat_input_text&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (activity != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; activity?.runOnUiThread {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val lparams = chat_textedit.layoutParams as ConstraintLayout.LayoutParams&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chat_textedit.layoutParams = lparams&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; override fun afterTextChanged(s: Editable) {}&nbsp; &nbsp; &nbsp; &nbsp; override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}&nbsp; &nbsp; }&nbsp; &nbsp; viewOfLayout.chat_input_text.addTextChangedListener(tw)}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java