android - 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()
会为我做到这一点,但它也没有。
解决方案
尝试这个
v.setOnFocusChangeListener(null)
v.clearFocus();
v.requestFocus();
v.selectAll()
v.setOnFocusChangeListener(listener)
推荐阅读
- ruby-on-rails - 用于自动完成多选(选择选项的复选框)和延迟加载的 Jquery 插件
- c++ - 未定义符号:_vkCreateInstance
- python - '在 /admin/filebrowser/browse/ 处配置不正确 查找上传文件夹时出错(site.storage.location + site.directory)。(Django 文件浏览器)
- pytorch - PyTorch:两个二进制掩码联合?
- reactjs - 在 NextJS 中搜索的最佳方式
- google-chrome-devtools - 在 Chrome DevTools 中找不到内存检查器
- python - 如何使用python将数据框中的列从小数转换为百分比
- python - 在 python 3 中使用 Counter 对键值对进行分组并获取计数
- amazon-web-services - 您请求的引擎模式无服务器当前在 5.7 (2.07.1) 中不可用
- raspberry-pi3 - “QWidget::paintEngine: 不应再被调用”错误语法