首页 > 解决方案 > EditText.selectAll() 不选择文本

问题描述

首先,如果我的问题有什么更好的地方,请告诉我,这是我的第一个问题。

我正在为android构建一个应用程序。

我希望我的用户能够在edittext字段中快速输入时间。通常那里已经有一个时间,应该选择它,这样键盘的任何输入都会导致它被覆盖,而只需点击“下一步”(或在键盘上输入“将保持原样并跳转到下一个字段.

对于我的所有领域,这都很好,但由于某种原因我无法掌握,无论我尝试什么,都不会选择这个特定领域(及其孪生)中的文本。

我放入android:selectAllOnFocus="true"res文件,当它不起作用时,我也放入v.selectAll()了它的onFocusChangedListener.

        flighttOutStringField?.onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
            if (hasFocus) {
                with(v as TextInputEditText) {
                    previousText = this.text.toString()
                    v.selectAll()
                }
            } else {
                with(v as TextInputEditText) {
                    val currentText = this.text.toString()
                    if (!(("([01]\\d|2[0-3]):[0-5]\\d".toRegex().containsMatchIn(currentText) && currentText.length == 5)
                                || ("([01]\\d|2[0-3])[0-5]\\d".toRegex().containsMatchIn(
                            currentText.padStart(
                                4,
                                '0'
                            )
                        ) && currentText.length <= 4))
                        || (currentText == "")
                    )
                        this.setText(previousText) // not a valid new entry, so no update on flight
                    else {
                        this.setText(
                            if (currentText.length < 5) "${currentText.padStart(
                                4,
                                '0')
                                .slice(0..1)}:${currentText.padStart(4, '0').slice(2..3)}" else currentText)
                        flight = flight.copy (tOutString = "${date?.format(DateTimeFormatter.ISO_DATE)}T${this.text}:00", changed = 1)
                    }
                }

            }

更新“flight”变量将通过其设置器重新填充所有字段。

                    <android.support.design.widget.TextInputLayout
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:id="@+id/flighttOutStringWrapper"

                            app:layout_constraintStart_toEndOf="@+id/flighttOutSelector" android:layout_marginTop="4dp"
                            app:layout_constraintTop_toBottomOf="@+id/flightOrigWrapper"
                            app:layout_constraintHorizontal_bias="0.5"
                            app:layout_constraintEnd_toStartOf="@+id/flighttInStringWrapper">
                        <android.support.design.widget.TextInputEditText
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:inputType="time"
                                android:ems="10"
                                android:id="@+id/flighttOutStringField"
                                android:textAlignment="center"
                                android:includeFontPadding="false"
                                android:hint="@string/timeOut"
                                android:nextFocusForward="@id/flighttInStringField" android:imeOptions="actionNext"
                                android:selectAllOnFocus="true"/>
                    </android.support.design.widget.TextInputLayout>

我认为在选择 时TextInputEditText,所有文本都会被选中,但事实并非如此。然后,我认为它v.selectAll()会为我做到这一点,但它也没有。

标签: androidkotlinandroid-edittext

解决方案


尝试这个

v.setOnFocusChangeListener(null) 
v.clearFocus();
v.requestFocus();
v.selectAll()
v.setOnFocusChangeListener(listener) 

推荐阅读