Форум для тех, кто программирует под Android
Anonymous
Курсор не движется после фильтрации текстового JetPack Compose
Сообщение
Anonymous » 11 авг 2025, 00:11
Я использую InputTransformation в JetPack Compose для фильтрации пользовательского ввода в BasicTextField . Преобразование работает правильно, но курсор остается в том же положении после входа в текст вместо движения назад. < /P>
Код: Выделить всё
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.text.input.InputTransformation
import androidx.compose.foundation.text.input.TextFieldBuffer
import androidx.compose.foundation.text.input.TextFieldLineLimits
import androidx.compose.foundation.text.input.TextFieldState
import androidx.compose.foundation.text.input.maxLength
import androidx.compose.foundation.text.input.rememberTextFieldState
import androidx.compose.foundation.text.input.then
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Text
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
val stateOne = rememberTextFieldState(initialText = "Hellow")
val stateTwo = rememberTextFieldState()
BasicTextFieldExamples(
stateTwo,
remember { MutableInteractionSource() },
)
}
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun BasicTextFieldExamples(
stateOTwo: TextFieldState,
secondInteractionSource: MutableInteractionSource,
) {
Column(
Modifier
.fillMaxSize()
.padding(50.dp)
) {
BasicTextField(
state = stateOTwo,
modifier = Modifier
.padding(top = 100.dp)
.fillMaxWidth()
.height(56.dp),
inputTransformation = InputTransformation.maxLength(6)
.then(LetterOnlyTransformation()),
interactionSource = secondInteractionSource,
decorator = TextFieldDefaults.decorator(
state = stateOTwo,
enabled = true,
label = {
Text("Last Name")
},
placeholder = {
Text("Example 2")
},
lineLimits = TextFieldLineLimits.Default,
interactionSource = secondInteractionSource,
outputTransformation = null
)
)
}
}
}
class LetterOnlyTransformation : InputTransformation {
override val keyboardOptions: KeyboardOptions?
get() = KeyboardOptions(keyboardType = KeyboardType.Text)
override fun TextFieldBuffer.transformInput() {
val filteredValue = asCharSequence().filter { it.isLetter() }
if (filteredValue != asCharSequence()) {
replace(0, length, filteredValue)
}
}
}
Как я могу убедиться, что курсор движется правильно после фильтрации ввода в inputtransformation ? Есть ли лучший способ справиться с этим сценарием в JetPack Compose?
Смотрите проблему здесь.
Подробнее здесь:
https://stackoverflow.com/questions/794 ... ck-compose
1754860263
Anonymous
Я использую InputTransformation в JetPack Compose для фильтрации пользовательского ввода в BasicTextField . Преобразование работает правильно, но курсор остается в том же положении после входа в текст вместо движения назад. < /P> [code]import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.activity.enableEdgeToEdge import androidx.compose.foundation.interaction.MutableInteractionSource import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.text.BasicTextField import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.foundation.text.input.InputTransformation import androidx.compose.foundation.text.input.TextFieldBuffer import androidx.compose.foundation.text.input.TextFieldLineLimits import androidx.compose.foundation.text.input.TextFieldState import androidx.compose.foundation.text.input.maxLength import androidx.compose.foundation.text.input.rememberTextFieldState import androidx.compose.foundation.text.input.then import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.Text import androidx.compose.material3.TextFieldDefaults import androidx.compose.runtime.Composable import androidx.compose.runtime.remember import androidx.compose.ui.Modifier import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.unit.dp class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() setContent { val stateOne = rememberTextFieldState(initialText = "Hellow") val stateTwo = rememberTextFieldState() BasicTextFieldExamples( stateTwo, remember { MutableInteractionSource() }, ) } } @OptIn(ExperimentalMaterial3Api::class) @Composable fun BasicTextFieldExamples( stateOTwo: TextFieldState, secondInteractionSource: MutableInteractionSource, ) { Column( Modifier .fillMaxSize() .padding(50.dp) ) { BasicTextField( state = stateOTwo, modifier = Modifier .padding(top = 100.dp) .fillMaxWidth() .height(56.dp), inputTransformation = InputTransformation.maxLength(6) .then(LetterOnlyTransformation()), interactionSource = secondInteractionSource, decorator = TextFieldDefaults.decorator( state = stateOTwo, enabled = true, label = { Text("Last Name") }, placeholder = { Text("Example 2") }, lineLimits = TextFieldLineLimits.Default, interactionSource = secondInteractionSource, outputTransformation = null ) ) } } } class LetterOnlyTransformation : InputTransformation { override val keyboardOptions: KeyboardOptions? get() = KeyboardOptions(keyboardType = KeyboardType.Text) override fun TextFieldBuffer.transformInput() { val filteredValue = asCharSequence().filter { it.isLetter() } if (filteredValue != asCharSequence()) { replace(0, length, filteredValue) } } } [/code] Как я могу убедиться, что курсор движется правильно после фильтрации ввода в inputtransformation ? Есть ли лучший способ справиться с этим сценарием в JetPack Compose? Смотрите проблему здесь. Подробнее здесь: [url]https://stackoverflow.com/questions/79430884/cursor-doesnt-move-after-filtering-text-jetpack-compose[/url]