Jetpack Compose TextField是否缺失flagNoPersonalizedLearning输入法选项?
Compose TextField 实现禁止输入法个性化学习的方案
- 原生Android的
EditText可通过flagNoPersonalizedLearning告知输入法不要学习该字段输入内容,这个功能在密码等敏感输入场景很实用。 - Compose的
KeyboardOptions默认的imeOptions枚举里确实没有直接对应的选项,但并非缺失该功能,可以通过以下方式实现:- 若使用较新版本的Compose(androidx.compose.ui:ui-text版本≥1.3.0),
ImeOptions类已内置FLAG_NO_PERSONALIZED_LEARNING常量,直接构建自定义ImeOptions即可:import androidx.compose.ui.text.input.ImeOptions import androidx.compose.ui.text.input.ImeAction import androidx.compose.ui.text.input.PasswordVisualTransformation // 构建包含禁止个性化学习flag的ImeOptions val customImeOptions = ImeOptions( action = ImeAction.Done, // 可根据需求设置其他IME动作 flags = ImeOptions.FLAG_NO_PERSONALIZED_LEARNING ) TextField( value = password, onValueChange = { password = it }, keyboardOptions = KeyboardOptions.Default.copy(imeOptions = customImeOptions), label = { Text("密码输入") }, visualTransformation = PasswordVisualTransformation() ) - 若使用旧版本Compose,可直接使用原生flag的数值(原生
IME_FLAG_NO_PERSONALIZED_LEARNING对应值为0x00001000)来设置:val customImeOptions = ImeOptions( action = ImeAction.Done, flags = 0x00001000 )
- 若使用较新版本的Compose(androidx.compose.ui:ui-text版本≥1.3.0),
内容的提问来源于stack exchange,提问作者pedronveloso
相关产品推荐
相关产品推荐

