Я создал пример кода, чтобы проиллюстрировать свою проблему.
Код: Выделить всё
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
InputInterceptTestTheme {
Fields()
}
}
}
}
@Composable
fun Fields() {
Column(
Modifier
.fillMaxSize()
.padding(10.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(text = "Field 1: NO KEYBOARD")
TestTextField(textFieldState = TextFieldState(), showKeyboard = false)
Text(text = "Field 2: SHOW KEYBOARD")
TestTextField(textFieldState = TextFieldState(), showKeyboard = true)
Text(text = "Field 3: NO KEYBOARD")
TestTextField(textFieldState = TextFieldState(), showKeyboard = false)
}
}
@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun TestTextField(
textFieldState: TextFieldState,
showKeyboard: Boolean,
) {
InterceptPlatformTextInput(interceptor = { request, nextHandler ->
if (showKeyboard) {
nextHandler.startInputMethod(request)
} else {
awaitCancellation()
}
}) {
BasicTextField(
state = textFieldState,
lineLimits = TextFieldLineLimits.SingleLine,
textStyle = TextStyle(
fontSize = 24.sp,
letterSpacing = 2.sp,
textAlign = TextAlign.Center,
fontFamily = FontFamily.Monospace
),
decorator = {
Box(
modifier = Modifier
.padding(bottom = 10.dp)
.fillMaxWidth()
.height(50.dp)
.background(Color.White, RoundedCornerShape(10.dp))
.border(1.dp, Color.Gray, RoundedCornerShape(10.dp)),
contentAlignment = Alignment.Center
) {
it()
}
}
)
}
}
Я не знаю. Я не понимаю большую часть InputConnections. Я думаю, что, поскольку они выполняют одно и то же действие, они используют какой-то сеанс ввода между полями, но я не знаю, как предотвратить такое поведение. Как я могу запретить отображение программной клавиатуры в BasicTextField после того, как другой BasicTextField показал ее в том же действии?
Подробнее здесь: https://stackoverflow.com/questions/785 ... basictextf
Мобильная версия