Получить выбранное количество текста в BasicTextField ⇐ Android
-
Anonymous
Получить выбранное количество текста в BasicTextField
I want to get BasicTextField selected text count in jetpack-compose, how can I do it? I want to check if any text is selected in the BasicTextField, show a dialog.
CustomBasicTextField( value = uiState.note.title, updateTextValue = { viewModel.updateNote(uiState.note.copy(title = it)) }, textStyle = MaterialTheme.typography.headlineSmall.copy(fontSize = 20.sp), placeholder = "Enter title", keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done), ) @Composable fun CustomBasicTextField( modifier: Modifier = Modifier, readOnly: Boolean = false, value: String, height: Dp = 54.dp, borderWidth: Dp = 1.dp, borderColor: Color = Color.Unspecified, borderShape: Shape = RoundedCornerShape(size = 5.dp), updateTextValue: (String) -> Unit, textStyle: TextStyle = TextStyle( fontSize = 16.sp, fontWeight = FontWeight.Medium, color = Color.DarkGray ), contentAlignment: Alignment = Alignment.CenterStart, visualTransformation: VisualTransformation = VisualTransformation.None, keyboardOptions: KeyboardOptions = KeyboardOptions.Default, keyboardActions: KeyboardActions = KeyboardActions.Default, singleLine: Boolean = false, label: @Composable (() -> Unit)? = null, placeholder: String = "", leadingIcon: @Composable (() -> Unit)? = null, trailingIcon: @Composable (() -> Unit)? = null, ) { Column(modifier = modifier.height(height)) { label?.let { it() } BasicTextField( modifier = modifier.fillMaxSize(), readOnly = readOnly, value = value, onValueChange = { newText -> updateTextValue(newText) }, visualTransformation = visualTransformation, keyboardActions = keyboardActions, keyboardOptions = keyboardOptions, singleLine = singleLine, textStyle = textStyle, decorationBox = { innerTextField -> Box( modifier = Modifier .fillMaxSize() .border( width = borderWidth, color = borderColor, shape = borderShape ) .padding(horizontal = 16.dp, vertical = 8.dp), contentAlignment = contentAlignment // Center the content vertically ) { Row( modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(4.dp) ) { leadingIcon?.let { it() } Box( modifier = Modifier .weight(1f) ) { if (value.isEmpty()) { Text( text = placeholder, style = LocalTextStyle.current.copy( fontSize = 16.sp, color = MaterialTheme.colorScheme.outline ), ) } innerTextField() } trailingIcon?.let { it() } } } } ) } } I tried this but not working: How do I get the selected text from SelectionContainer
Источник: https://stackoverflow.com/questions/780 ... ctextfield
I want to get BasicTextField selected text count in jetpack-compose, how can I do it? I want to check if any text is selected in the BasicTextField, show a dialog.
CustomBasicTextField( value = uiState.note.title, updateTextValue = { viewModel.updateNote(uiState.note.copy(title = it)) }, textStyle = MaterialTheme.typography.headlineSmall.copy(fontSize = 20.sp), placeholder = "Enter title", keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done), ) @Composable fun CustomBasicTextField( modifier: Modifier = Modifier, readOnly: Boolean = false, value: String, height: Dp = 54.dp, borderWidth: Dp = 1.dp, borderColor: Color = Color.Unspecified, borderShape: Shape = RoundedCornerShape(size = 5.dp), updateTextValue: (String) -> Unit, textStyle: TextStyle = TextStyle( fontSize = 16.sp, fontWeight = FontWeight.Medium, color = Color.DarkGray ), contentAlignment: Alignment = Alignment.CenterStart, visualTransformation: VisualTransformation = VisualTransformation.None, keyboardOptions: KeyboardOptions = KeyboardOptions.Default, keyboardActions: KeyboardActions = KeyboardActions.Default, singleLine: Boolean = false, label: @Composable (() -> Unit)? = null, placeholder: String = "", leadingIcon: @Composable (() -> Unit)? = null, trailingIcon: @Composable (() -> Unit)? = null, ) { Column(modifier = modifier.height(height)) { label?.let { it() } BasicTextField( modifier = modifier.fillMaxSize(), readOnly = readOnly, value = value, onValueChange = { newText -> updateTextValue(newText) }, visualTransformation = visualTransformation, keyboardActions = keyboardActions, keyboardOptions = keyboardOptions, singleLine = singleLine, textStyle = textStyle, decorationBox = { innerTextField -> Box( modifier = Modifier .fillMaxSize() .border( width = borderWidth, color = borderColor, shape = borderShape ) .padding(horizontal = 16.dp, vertical = 8.dp), contentAlignment = contentAlignment // Center the content vertically ) { Row( modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(4.dp) ) { leadingIcon?.let { it() } Box( modifier = Modifier .weight(1f) ) { if (value.isEmpty()) { Text( text = placeholder, style = LocalTextStyle.current.copy( fontSize = 16.sp, color = MaterialTheme.colorScheme.outline ), ) } innerTextField() } trailingIcon?.let { it() } } } } ) } } I tried this but not working: How do I get the selected text from SelectionContainer
Источник: https://stackoverflow.com/questions/780 ... ctextfield
Мобильная версия