網(wǎng)站首頁 編程語言 正文
概覽
眾所周知Compose中默認的TextField和OutlineTextField樣式并不能滿足所有的使用場景,所以自定義TextField就成了必備技能,本文就揭露一下自定義TextField的實現(xiàn)。自定義TextField主要使用BasicTextField實現(xiàn)。
簡單自定義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.效果
實現(xiàn)自定義樣式的BasicTextField
我們知道BasicTextField提供了基礎(chǔ)的輸入框,只包含文字輸入,光標等簡單功能,如果我們需要增加樣式就需要自己實現(xiàn)decorationBox參數(shù),來添加樣式。
本例中我們實現(xiàn)一個帶藍色邊框,內(nèi)部填充綠色,左側(cè)有圖標的自定義BasicTextField。
1.代碼
@Composable fun 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內(nèi)部負責編寫輸入框樣式 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()//自定義樣式這行代碼是關(guān)鍵,沒有這一行輸入文字后無法展示,光標也看不到 } } } ) } }
2.效果
使用BasicTextField自定義百度輸入框
1.代碼
@Composable fun 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.效果
原文鏈接:https://blog.csdn.net/ymeddmn/article/details/122263642
相關(guān)推薦
- 2022-04-01 docker 命令查看registry倉庫鏡像
- 2022-08-21 android實現(xiàn)貝塞爾曲線之波浪效果_Android
- 2022-06-02 如何利用C#正則表達式判斷是否是有效的文件及文件夾路徑_C#教程
- 2022-10-07 Golang標準庫unsafe源碼解讀_Golang
- 2022-12-23 一文帶你搞懂Python中的文件操作_python
- 2022-09-12 關(guān)于Rsa?Public?Key?not?Find的問題及解決_數(shù)據(jù)庫其它
- 2022-07-01 淺談為什么#{}可以防止SQL注入_MsSql
- 2022-07-30 Linux secure 日志分析
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支