Clear keyboard focus once inputs are done

pull/1/head
Lonami Exo 2022-10-11 15:43:37 +02:00
parent e8624b5635
commit f8fcf8b642
1 changed files with 8 additions and 0 deletions

View File

@ -4,11 +4,13 @@ import android.os.Bundle
import androidx.activity.ComponentActivity import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.* import androidx.compose.material.*
import androidx.compose.runtime.* import androidx.compose.runtime.*
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.ImeAction import androidx.compose.ui.text.input.ImeAction
@ -45,6 +47,8 @@ fun isLoginCodeValid(code: String): Boolean = code.trim().count { it.isDigit() }
@Composable @Composable
fun PhoneInput(phone: String, onPhoneChanged: (String) -> Unit, onSendCode: () -> Unit) { fun PhoneInput(phone: String, onPhoneChanged: (String) -> Unit, onSendCode: () -> Unit) {
val focusManager = LocalFocusManager.current
Text(stringResource(R.string.enter_phone)) Text(stringResource(R.string.enter_phone))
TextField( TextField(
phone, phone,
@ -55,6 +59,7 @@ fun PhoneInput(phone: String, onPhoneChanged: (String) -> Unit, onSendCode: () -
keyboardType = KeyboardType.Phone, keyboardType = KeyboardType.Phone,
imeAction = ImeAction.Done imeAction = ImeAction.Done
), ),
keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() }),
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
onValueChange = onPhoneChanged onValueChange = onPhoneChanged
) )
@ -70,6 +75,8 @@ fun PhoneInput(phone: String, onPhoneChanged: (String) -> Unit, onSendCode: () -
@Composable @Composable
fun OtpInput(otp: String, onOtpChanged: (String) -> Unit, onConfirmOtp: () -> Unit) { fun OtpInput(otp: String, onOtpChanged: (String) -> Unit, onConfirmOtp: () -> Unit) {
val focusManager = LocalFocusManager.current
Text(stringResource(R.string.enter_otp)) Text(stringResource(R.string.enter_otp))
TextField( TextField(
otp, otp,
@ -80,6 +87,7 @@ fun OtpInput(otp: String, onOtpChanged: (String) -> Unit, onConfirmOtp: () -> Un
keyboardType = KeyboardType.Number, keyboardType = KeyboardType.Number,
imeAction = ImeAction.Done imeAction = ImeAction.Done
), ),
keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() }),
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
onValueChange = onOtpChanged onValueChange = onOtpChanged
) )