我正在制作一个计算器来学习 Compose,因此我在屏幕上放置了自己的数字按钮,并且我想防止软键盘出现。
这是我的仓库:https://github.com/vitor-ramos/CalculadorCompose
我注意到 TextFieldImpl.kt 中有一个用于显示键盘的修饰符,因此我尝试克隆代码并删除该行:keyboardController.value?.showSoftwareKeyboard()
我知道重复这样的代码不是一个好主意,但我想尝试一下,但没有成功。正如您在下面的原始代码中看到的,有一个 TODO 说它应该由 BaseTextField 处理,但我查看了它的代码,没有找到它显示或隐藏键盘的位置。
val textFieldModifier = modifier
.focusRequester(focusRequester)
.focusObserver { isFocused = it.isFocused }
.clickable(indication = null) {
focusRequester.requestFocus()
// TODO(b/163109449): Showing and hiding keyboard should be handled by BaseTextField.
// The requestFocus() call here should be enough to trigger the software keyboard.
// Investiate why this is needed here. If it is really needed, instead of doing
// this in the onClick callback, we should move this logic to the focusObserver
// so that it can show or hide the keyboard based on the focus state.
keyboardController.value?.showSoftwareKeyboard()
}
我在这个问题中发现,通过视图我可以扩展 EditText 并更改功能,但我还没有找到 Compose 的等效项:Android:禁用所有 EditText 的软键盘
public class NoImeEditText extends EditText {
public NoImeEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onCheckIsTextEditor() {
return false;
}
}
我已经测试了 Arun Pdiyan 解决方案,并且像带有 null LocalTextInputService 的魅力一样工作(在我的例子中,我从设备连接的条形码阅读器读取数据)
CompositionLocalProvider(
LocalTextInputService provides null
) {
TextField(
value = barcodeReaderService.readedText.value,
onValueChange = { textState.value=it },
label = { Text("The Label") }
)
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)