до 1.7.0 Это было: < /p>
Код: Выделить всё
val textInputService = LocalTextInputService.current
< /code>
и после 1.7.0 стало: < /p>
val legacyTextInputServiceAdapter = remember { createLegacyPlatformTextInputServiceAdapter() }
val textInputService: TextInputService = remember {
TextInputService(legacyTextInputServiceAdapter)
}
Контейнер выше всех приложений Composable: < /li>
< /ol>
Код: Выделить всё
@Composable
fun MyKeyboardContainer(
modifier: Modifier = Modifier,
closeKeyboardClickOutside: Boolean = true,
content: @Composable ColumnScope.() -> Unit,
) = Column(modifier = modifier) {
val focusManager = LocalFocusManager.current
val clickableModifier = if (closeKeyboardClickOutside) {
Modifier.clickable(
interactionSource = remember { MutableInteractionSource() },
indication = null
) { focusManager.clearFocus(force = true) }
} else {
Modifier
}
val textInputService: TextInputService = MyLocalTextInputService.current
CompositionLocalProvider(LocalTextInputService provides textInputService) {
Column(
modifier = Modifier
.weight(1f)
.fillMaxWidth()
.then(clickableModifier),
content = content,
)
MyKeyboard(windowSize)
}
}
< /code>
textInputService: < /li>
< /ol>
/**
* Static composition local variable for holding application built-in [TextInputService]
*/
internal val MyLocalTextInputService =
staticCompositionLocalOf { MyTextInputService(MyPlatformTextInputService()) }
/**
* Extends [TextInputService]. Replaces [PlatformTextInputService] with
* [MyPlatformTextInputService] to show application built-in keyboard instead of
* android system keyboard.
*/
internal class MyTextInputService(
val mPlatformTextInputService: MyPlatformTextInputService
) : TextInputService(mPlatformTextInputService)
/**
* Application text input service. This class is responsible for holding [InputState] and
* [KeyboardState]
*/
internal class MyPlatformTextInputService : PlatformTextInputService {
val softKeyboardState = MutableStateFlow(KeyboardState.HIDDEN)
val inputState = MutableStateFlow(InputState.Idle)
override fun hideSoftwareKeyboard() {
softKeyboardState.value = KeyboardState.HIDDEN
}
override fun showSoftwareKeyboard() {
softKeyboardState.value = KeyboardState.SHOWN
}
override fun startInput(
value: TextFieldValue,
imeOptions: ImeOptions,
onEditCommand: (List) -> Unit,
onImeActionPerformed: (ImeAction) -> Unit,
) {
inputState.value = InputState.Active(
textFieldValue = value,
onEditCommand = onEditCommand,
onImeActionPerformed = onImeActionPerformed,
imeOptions = imeOptions,
)
softKeyboardState.value = KeyboardState.SHOWN
}
override fun stopInput() {
inputState.value = InputState.Idle
}
override fun updateState(oldValue: TextFieldValue?, newValue: TextFieldValue) {
(inputState.value as? InputState.Active)?.let { activeInputState ->
inputState.value = activeInputState.copy(textFieldValue = newValue)
}
}
enum class KeyboardState {
HIDDEN, SHOWN,
}
sealed interface InputState {
object Idle : InputState
data class Active(
val textFieldValue: TextFieldValue,
val onEditCommand: (List) -> Unit,
val onImeActionPerformed: (ImeAction) -> Unit,
val imeOptions: ImeOptions,
) : InputState
}
}
Подробнее здесь: https://stackoverflow.com/questions/792 ... -and-above
Мобильная версия