Класс PasswordVisualTransformation
Класс PasswordVisualTransformation
из пакета androidx.compose.ui.text.input
преобразует отображаемый текст так,
что вместо букв пользователь видит маску.
Объект передают в параметр visualTransformation
у OutlinedTextField
и TextField.
Строка в value остаётся настоящей,
меняется только отрисовка на экране.
Для типа клавиатуры часто ставят
KeyboardType
Password или NumberPassword.
Синтаксис
PasswordVisualTransformation()
Пример
Давайте спрячем ввод в OutlinedTextField:
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.PasswordVisualTransformation
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
var text by remember { mutableStateOf("abcde") }
OutlinedTextField(
value = text,
onValueChange = { text = it },
label = { Text("abcde") },
visualTransformation = PasswordVisualTransformation(),
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Password,
),
)
}
}
}
На экране в поле видны точки или звёздочки,
а не открытые символы строки "abcde".
Смотрите также
-
функция
OutlinedTextField,
которая принимает visualTransformation у поля -
функция
TextField,
которая тоже маскирует пароль через visualTransformation -
класс
KeyboardType,
у которого есть значения Password и NumberPassword -
класс
KeyboardOptions,
который передаёт keyboardType вместе с другими настройками