Waq*_*hir 10 android android-jetpack-compose android-jetpack-compose-list
我想在待办事项列表中创建一个文本字段when the user presses backspace on an empty text field , it removes itself,如您所见,非常简单!
如果按键事件是可编辑的,则 Jetpack Compose 和 Core Text Field 不会将其按键事件传播到父可组合项/修饰符 ~~ 在其代码中编写
所以我尝试了这个,但它没有按预期工作
Modifier.onKeyEvent {
Log.d("BL_KeyEvent",it.type.toString())
if (it.key.keyCode == Key.Backspace.keyCode) {
if (item.text.isEmpty()) {
onBackspaceRemove()
}
}
false
}
Run Code Online (Sandbox Code Playgroud)
我只是想知道如何实现它,因为 CoreTextField 是内部的,我无法捕获文本字段中的关键事件
如果自您上次尝试 @Waqas Tahir 以来此问题已得到解决,请取消请求,但我正在使用 ComposeUI v1.0.2,以下内容对我有用:
@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun aTextFieldWithOnBackspacePressedAction() {
// this should be hoisted to ViewModel
var text by remember { mutableStateOf("") }
BasicTextField(
value = text,
// this also should be hoisted to ViewModel
onValueChange = { text = it },
modifier = Modifier
.onKeyEvent { event: KeyEvent ->
// handle backspace key
if (event.type == KeyEventType.KeyUp &&
event.key == Key.Backspace &&
text.isEmpty()
// also any additional checks of the "list" i.e isNotEmpty()
) {
// TODO remove from list
return@onKeyEvent true
}
false
}
)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6216 次 |
| 最近记录: |