From 59839fe8caa9b7cd28ab80afa5160209917e16e4 Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Wed, 14 Aug 2024 13:29:06 +0200 Subject: [PATCH] auto focus --- .../eu/m724/coincounter/WalletActivity.kt | 48 ++++++++++++++----- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/eu/m724/coincounter/WalletActivity.kt b/app/src/main/java/eu/m724/coincounter/WalletActivity.kt index 4dfaeda..f225c7e 100644 --- a/app/src/main/java/eu/m724/coincounter/WalletActivity.kt +++ b/app/src/main/java/eu/m724/coincounter/WalletActivity.kt @@ -29,15 +29,22 @@ import androidx.compose.material3.Text import androidx.compose.material3.TextButton import androidx.compose.material3.TextField import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment +import androidx.compose.ui.ExperimentalComposeUiApi import androidx.compose.ui.Modifier +import androidx.compose.ui.focus.FocusRequester +import androidx.compose.ui.focus.focusProperties +import androidx.compose.ui.focus.focusRequester import androidx.compose.ui.graphics.Color import androidx.compose.ui.input.pointer.pointerInput +import androidx.compose.ui.text.input.ImeAction import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp @@ -198,13 +205,19 @@ fun TransactionList(viewModel: WalletViewModel) { } +@OptIn(ExperimentalComposeUiApi::class) @Composable fun TransactionDialog( onDismiss: () -> Unit, onConfirm: (String, Int) -> Unit ) { var label by rememberSaveable { mutableStateOf("") } - var value by rememberSaveable { mutableStateOf("0") } + var value by rememberSaveable { mutableStateOf("") } + val (firstFocus, secondFocus) = remember { FocusRequester.createRefs() } + + LaunchedEffect(Unit) { + firstFocus.requestFocus() + } Dialog( onDismissRequest = { @@ -225,8 +238,13 @@ fun TransactionDialog( onValueChange = { label = it }, modifier = Modifier .fillMaxWidth() - .weight(1f), + .weight(1f) + .focusRequester(firstFocus) + .focusProperties { next = secondFocus }, supportingText = { Text("Label") }, + keyboardOptions = KeyboardOptions( + imeAction = ImeAction.Next + ), singleLine = true ) Spacer(modifier = Modifier.width(6.dp)) @@ -235,9 +253,13 @@ fun TransactionDialog( onValueChange = { value = it }, modifier = Modifier .fillMaxWidth() - .weight(1f), + .weight(1f) + .focusRequester(secondFocus), supportingText = { Text("Value") }, - keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), + keyboardOptions = KeyboardOptions( + keyboardType = KeyboardType.Number, + imeAction = ImeAction.Done + ), singleLine = true ) } @@ -245,15 +267,19 @@ fun TransactionDialog( modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.End ) { - TextButton(onClick = { - label = "" - onDismiss() - }) { + TextButton( + onClick = { + label = "" + onDismiss() + } + ) { Text(text = "Cancel") } - TextButton(onClick = { - onConfirm(label, (value.toFloat() * 100).toInt()) - }) { + TextButton( + onClick = { + onConfirm(label, (value.toFloat() * 100).toInt()) + } + ) { Text("Create transaction") } }