Android Compose自定义TextField如何实现自定义的输入框
发表于:2025-11-08 作者:千家信息网编辑
千家信息网最后更新 2025年11月08日,这篇文章主要介绍Android Compose自定义TextField如何实现自定义的输入框,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!简单自定义BasicTextField
千家信息网最后更新 2025年11月08日Android Compose自定义TextField如何实现自定义的输入框
这篇文章主要介绍Android Compose自定义TextField如何实现自定义的输入框,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
简单自定义BasicTextField示例
1.代码
var text by remember { mutableStateOf("简单的TextField") } BasicTextField( value = text, onValueChange = { text = it }, modifier = Modifier .height(40.dp) .width(300.dp) .background(Color.Green) )2.效果
实现自定义样式的BasicTextField
我们知道BasicTextField提供了基础的输入框,只包含文字输入,光标等简单功能,如果我们需要增加样式就需要自己实现decorationBox参数,来添加样式。
本例中我们实现一个带蓝色边框,内部填充绿色,左侧有图标的自定义BasicTextField。
1.代码
@Composablefun DecorateTextField() { var text by rememberSaveable { mutableStateOf("init") } Box( Modifier .fillMaxWidth() .fillMaxHeight(), contentAlignment = Alignment.Center ) { BasicTextField( value = text, onValueChange = { text = it }, textStyle = TextStyle(color = Color.White), cursorBrush = SolidColor(Color.Blue), decorationBox = { innerTextField ->//decorationBox内部负责编写输入框样式 Row( Modifier .padding(2.dp) .fillMaxWidth() .background(Color.Blue, RoundedCornerShape(percent = 30)) .padding(1.dp) .background(Color.Green, RoundedCornerShape(percent = 29)), verticalAlignment = Alignment.CenterVertically ) { Icon(Icons.Default.Star, tint = Color.White, contentDescription = null) Spacer(Modifier.width(5.dp)) Box(modifier = Modifier.padding(top = 7.dp, bottom = 7.dp, end = 7.dp)) { innerTextField()//自定义样式这行代码是关键,没有这一行输入文字后无法展示,光标也看不到 } } } ) }}2.效果
使用BasicTextField自定义百度输入框
1.代码
@Composablefun BaiduTextField() { var text by remember { mutableStateOf("安安安安卓") } BasicTextField(value = text, onValueChange = { text = it }, decorationBox = { innerTextField -> val iconModifier = Modifier.padding(start = 5.dp) Row( modifier = Modifier .padding(horizontal = 5.dp, vertical = 3.dp) .fillMaxWidth() .height(50.dp) .padding(start = 5.dp) .border(width = 1.dp, color = Color.Blue, shape = RoundedCornerShape(8.dp)) ) { Box( modifier = Modifier .padding(start = 8.dp) .weight(1f) .fillMaxHeight() , contentAlignment = Alignment.CenterStart ) { innerTextField() } Row( modifier = Modifier.fillMaxHeight(), verticalAlignment = Alignment.CenterVertically ) { Icon( painter = painterResource(id = R.drawable.cha), "", modifier = iconModifier.size(20.dp), tint = Color.Gray ) Icon( painter = painterResource(id = R.drawable.record), "", modifier = iconModifier.size(20.dp), tint = Color.Gray ) Icon( painter = painterResource(id = R.drawable.takepic), "", modifier = iconModifier.padding(end = 8.dp).size(20.dp), tint = Color.Gray ) Box( modifier = Modifier .width(120.dp) .fillMaxHeight() .background( color = Color.Blue, shape = RoundedCornerShape(topEnd = 8.dp, bottomEnd = 8.dp) ).clickable { }, contentAlignment = Alignment.Center ) { Text( text = "百度一下", color = Color.White ) } } } })}2.效果
以上是"Android Compose自定义TextField如何实现自定义的输入框"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!
输入
样式
代码
效果
光标
内容
文字
篇文章
一行
价值
关键
兴趣
功能
参数
图标
基础
小伙
小伙伴
更多
知识
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
安卓.db .gpkg数据库
计算机网络技术的就业规划
北仑敏捷软件开发服务
足球经理数据库影响啥
六安市网络技术及信息安全工程师
数据库管理系统php
清朗网络安全日
制作长图软件开发
btw软件开发
动漫数据库 关系
云服务器安全狗怎么样
武汉大学网络安全试题
网络安全工具简笔画图片
网络安全人员考试试题
学网络安全要什么条件
辽宁超级服务器云主机
初中生软件开发
银川黄东路计算机网络技术学校
通过网络安全知识我知道了
药易通软件开发公司
中国知网博士硕士论文数据库
宁波鄞州区财务软件开发价格
网络安全文章PDF
张宏业简历网络安全
pubg怎么知道在哪个服务器
什么还是5g网络技术
税务 机构改革 网络安全
服务器橙色是什么意思
锐捷网络技术支持笔试
网络安全 网络发展趋势