当前位置:网站首页>Jetpack Compose——TextField及OutlinedTextField(文本框)的使用
Jetpack Compose——TextField及OutlinedTextField(文本框)的使用
2022-08-09 13:12:00 【lplj717】
TextField的构造
@Composable
fun TextField(
//输入的文本内容
value: String,
//更新文本触发回调,类似EditText中addTextChangedListener
onValueChange: (String) -> Unit,
//样式
modifier: Modifier = Modifier,
//控制TextField的启用状态,false:文本字段不可编辑不可聚焦
enabled: Boolean = true,
//控制TextField的可编辑状态
readOnly: Boolean = false,
//文字样式,类型为TextStyle
textStyle: TextStyle = LocalTextStyle.current,
//文本字段容器中显示的可选标签
label: @Composable (() -> Unit)? = null,
//占位符,TextView中配置的hint属性一样,需要注意的是如何和label一起使用,会被覆盖效果,只有获取焦点时候才会被显示出来
placeholder: @Composable (() -> Unit)? = null,
//前导图标,文本前的图标
leadingIcon: @Composable (() -> Unit)? = null,
//尾随图标,文本后的图标
trailingIcon: @Composable (() -> Unit)? = null,
//当前值是否错误
isError: Boolean = false,
//视觉转换,类似EditText中的inputType,实现VisualTransformation该接口来实现一些其他转换
visualTransformation: VisualTransformation = VisualTransformation.None,
//输入框类型。可以定义为return/search等
keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
//输入框Enter键盘类型,键盘返回键的的回调
keyboardActions: KeyboardActions = KeyboardActions(),
//是否单行
singleLine: Boolean = false,
//最大行
maxLines: Int = Int.MAX_VALUE,
//组件交互变化
//interactionSource 处理状态的属性,比如按下的时候什么效果,正常时候什么效果,获取焦点时候什么效果等。类似之前再布局文件里写Selector。
//interactionSource.collectIsPressedAsState() 判断是否按下状态
//interactionSource.collectIsFocusedAsState() 判断是否获取焦点的状态
//interactionSource.collectIsDraggedAsState() 判断是否拖动
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
//文本框形状
shape: Shape =
MaterialTheme.shapes.small.copy(bottomEnd = ZeroCornerSize, bottomStart = ZeroCornerSize),
//文字,光标等颜色
colors: TextFieldColors = TextFieldDefaults.textFieldColors()
)
由于OutlinedTextField和TextField属性差不多,所以就不过多注释了
@Composable
fun OutlinedTextField(
value: TextFieldValue,
onValueChange: (TextFieldValue) -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
readOnly: Boolean = false,
textStyle: TextStyle = LocalTextStyle.current,
label: @Composable (() -> Unit)? = null,
placeholder: @Composable (() -> Unit)? = null,
leadingIcon: @Composable (() -> Unit)? = null,
trailingIcon: @Composable (() -> Unit)? = null,
isError: Boolean = false,
visualTransformation: VisualTransformation = VisualTransformation.None,
keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
keyboardActions: KeyboardActions = KeyboardActions(),
singleLine: Boolean = false,
maxLines: Int = Int.MAX_VALUE,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
shape: Shape = MaterialTheme.shapes.small,
colors: TextFieldColors = TextFieldDefaults.outlinedTextFieldColors()
)
接下来看看基本使用:
TextField(value = "Ellen Li", onValueChange = {}, label = { Text(text = "userName") })
效果如图:
出现问题:文本框内容并没有被改变,此时焦点是可以获取到的,但是修改的文本并没有显示在文本框中,换句话说就是TextField不可用,这就和EditText不同,TextField要求我们自己处理内容更新问题,所以我们处理一下:(注意定义的text变量是可观察变量)
var text by remember {
mutableStateOf("")//可观察的文本
}
TextField(
value = text,//显示文本
onValueChange = { text = it },//文本修改
label = { Text(text = "userName") },
leadingIcon = { Icon(Icons.Filled.Search, contentDescription = null) },//文本前图片
trailingIcon = {
Image(
painter = painterResource(id = R.mipmap.ico_clean),
contentDescription = null,
modifier = Modifier.clickable { text = "" })//按钮点击事件
},//文本后图片
)
效果如图:
OutlinedTextField基本使用:
var inputText by remember {
mutableStateOf("")//可观察的文本
}
OutlinedTextField(
value = inputText,
onValueChange = { inputText = it },
label = { Text(text = "这里是标签") },
placeholder = { Text(text = "请输入内容") },
trailingIcon = {
Image(
painter = painterResource(id = R.mipmap.ico_clean),
contentDescription = null,
modifier = Modifier.clickable { inputText = "" })//按钮点击事件
}
)
效果如图:
接下来介绍一下视图变化:visualTransformation
var inputT by remember {
mutableStateOf("")//输入文本
}
var isVisible by remember {
mutableStateOf(false)//是否密码显示
}
//定义可见变换
val myVisibleTransform =
if (isVisible) VisualTransformation.None else PasswordVisualTransformation()
TextField(
value = inputT,
colors = TextFieldDefaults.textFieldColors(backgroundColor = Color.Transparent),
onValueChange = { value -> inputT = value },
visualTransformation = myVisibleTransform,
trailingIcon = {
//根据表示不同,显示不同的图标
if (isVisible) {
IconButton(onClick = {
isVisible = !isVisible
}) {
Icon(painter = painterResource(id = R.mipmap.icon_visible), null)
}
} else {
IconButton(onClick = {
isVisible = !isVisible
}) {
Icon(painter = painterResource(id = R.mipmap.icon_invisible), null)
}
}
}
)
效果如图:
TextField的简单介绍基就到这里了,最后附上所有内容代码:
@Composable
fun TestTextFiled() {
Column(
Modifier
.fillMaxWidth()
.padding(20.dp)
.verticalScroll(state = rememberScrollState())
) {
TextField(value = "Ellen Li", onValueChange = {}, label = { Text(text = "userName") })
var text by remember {
mutableStateOf("")//可观察的文本
}
TextField(
value = text,//显示文本
onValueChange = { text = it },//文本修改
label = { Text(text = "userName") },
leadingIcon = { Icon(Icons.Filled.Search, contentDescription = null) },//文本前图片
trailingIcon = {
Image(
painter = painterResource(id = R.mipmap.ico_clean),
contentDescription = null,
modifier = Modifier.clickable { text = "" })//按钮点击事件
},//文本后图片
)
Spacer(modifier = Modifier.padding(5.dp))
var inputText by remember {
mutableStateOf("")//可观察的文本
}
OutlinedTextField(
value = inputText,
onValueChange = { inputText = it },
label = { Text(text = "这里是标签") },
placeholder = { Text(text = "请输入内容") },
trailingIcon = {
Image(
painter = painterResource(id = R.mipmap.ico_clean),
contentDescription = null,
modifier = Modifier.clickable { inputText = "" })//按钮点击事件
}
)
Spacer(modifier = Modifier.padding(5.dp))
var inputT by remember {
mutableStateOf("")//输入文本
}
var isVisible by remember {
mutableStateOf(false)//是否密码显示
}
//定义可见变换
val myVisibleTransform =
if (isVisible) VisualTransformation.None else PasswordVisualTransformation()
TextField(
value = inputT,
colors = TextFieldDefaults.textFieldColors(backgroundColor = Color.Transparent),
onValueChange = { value -> inputT = value },
visualTransformation = myVisibleTransform,
trailingIcon = {
//根据表示不同,显示不同的图标
if (isVisible) {
IconButton(onClick = {
isVisible = !isVisible
}) {
Icon(painter = painterResource(id = R.mipmap.icon_visible), null)
}
} else {
IconButton(onClick = {
isVisible = !isVisible
}) {
Icon(painter = painterResource(id = R.mipmap.icon_invisible), null)
}
}
}
)
}
}
边栏推荐
- An Offer 21. Adjust the array in order to make odd in even the front (loop invariant)
- error Trailing spaces not allowed no-trailing-spaces 9:14 error Unexpected trailing comma
- RobotFramework 之 用户关键字
- opencv-matchTemplate 之使用场景为大图里面找小图
- WPF 系统托盘 图标闪烁
- Q_06_02 类型模型
- NC61 两数之和
- 目标检测基础
- ArcEngine(十)创建矢量图层
- Professor Chen Qiang's "Machine Learning and R Application" course Chapter 13 Assignment
猜你喜欢
蓝桥杯线上模拟赛——Flex 经典骰子布局
Ledong Fire Rescue Brigade was invited to carry out fire safety training for cadres
群组行动控制--自动队列化实现策略
error Trailing spaces not allowed no-trailing-spaces 9:14 error Unexpected trailing comma
pytest 之 allure报告
关于做2D游戏时,Canvas边界显示在Game窗口的问题
问题系列-如何修改或更新localhost里的值
tkiner-canvas显示图片
面试攻略系列(四)-- 你不知道的大厂面试
GIN file upload and return
随机推荐
神经网络与深度学习(TensorFlow)
pytest 之 fixture的定义及作用域
IDEA Gradle 常遇问题(二)(持续更新)
Professor Chen Qiang the machine learning and R application course chapter 18 assignments
Map mixed density function and quantile added line
RobotFramework 之 RF变量与标准库关键字使用
分布式系统关注点(8)——99%的人都能看懂的「熔断」以及最佳实践 (转载非原创)
二叉树的遍历(py)
面试攻略系列(三)-- 高级开发工程师面试问些啥?
面试攻略系列(四)-- 你不知道的大厂面试
【面试高频题】可逐步优化的链表高频题
JS动画函数封装
易语言获取cookie
offset、client、scroll、window.pageYOffset比较
GIN file upload and return
哈希表卡片
pytest 与 unittest 的区别
Record the system calls and C library functions used in this project-2
什么是布隆过滤器?如何使用?
LeetCode 37. Solve Sudoku