Android Compose自定義TextField實(shí)現(xiàn)自定義的輸入框
概覽
眾所周知Compose中默認(rèn)的TextField和OutlineTextField樣式并不能滿足所有的使用場(chǎng)景,所以自定義TextField就成了必備技能,本文就揭露一下自定義TextField的實(shí)現(xiàn)。自定義TextField主要使用BasicTextField實(shí)現(xiàn)。
簡(jiǎn)單自定義BasicTextField示例
1.代碼
var text by remember { mutableStateOf("簡(jiǎn)單的TextField") } BasicTextField( value = text, onValueChange = { text = it }, modifier = Modifier .height(40.dp) .width(300.dp) .background(Color.Green) )
2.效果
實(shí)現(xiàn)自定義樣式的BasicTextField
我們知道BasicTextField提供了基礎(chǔ)的輸入框,只包含文字輸入,光標(biāo)等簡(jiǎn)單功能,如果我們需要增加樣式就需要自己實(shí)現(xiàn)decorationBox參數(shù),來(lái)添加樣式。
本例中我們實(shí)現(xiàn)一個(gè)帶藍(lán)色邊框,內(nèi)部填充綠色,左側(cè)有圖標(biāo)的自定義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)部負(fù)責(zé)編寫(xiě)輸入框樣式 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)鍵,沒(méi)有這一行輸入文字后無(wú)法展示,光標(biāo)也看不到 } } } ) } }
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.效果
以上就是Android Compose自定義TextField實(shí)現(xiàn)自定義的輸入框的詳細(xì)內(nèi)容,更多關(guān)于Compose TextField自定義輸入框的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
android中AutoCompleteTextView的簡(jiǎn)單用法(實(shí)現(xiàn)搜索歷史)
本篇文章主要介紹了android中AutoCompleteTextView的簡(jiǎn)單用法(自動(dòng)提示),有需要的可以了解一下。2016-11-11Android音頻開(kāi)發(fā)之SurfaceView的使用詳解
這篇文章主要為大家介紹了Android中SurfaceView的使用方法,本文通過(guò)簡(jiǎn)要的案例,為大家進(jìn)行了詳細(xì)的講解,需要的朋友可以參考一下2022-04-04android中實(shí)現(xiàn)editext搜索完成自動(dòng)關(guān)閉軟鍵盤(pán)
在Android應(yīng)用開(kāi)發(fā)中,經(jīng)常會(huì)遇到需要在EditText中輸入內(nèi)容,并通過(guò)搜索按鈕進(jìn)行搜索的場(chǎng)景,通常情況下,當(dāng)用戶點(diǎn)擊搜索按鈕后,我們希望關(guān)閉軟鍵盤(pán)以提供更好的用戶體驗(yàn),本文將介紹如何在Android中實(shí)現(xiàn)EditText搜索完成后自動(dòng)關(guān)閉軟鍵盤(pán)的功能2023-10-10一文詳解?Compose?Navigation?的實(shí)現(xiàn)原理
這篇文章主要介紹了一文詳解?Compose?Navigation的實(shí)現(xiàn)原理,文章通告圍繞主題展開(kāi)詳細(xì)的相關(guān)內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08Android使用HorizontalScrollView實(shí)現(xiàn)水平滾動(dòng)
這篇文章主要為大家詳細(xì)介紹了Android使用HorizontalScrollView實(shí)現(xiàn)水平滾動(dòng),并點(diǎn)擊有相應(yīng)的反應(yīng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11Android監(jiān)聽(tīng)來(lái)電和去電的實(shí)現(xiàn)方法
這篇文章主要介紹了Android監(jiān)聽(tīng)來(lái)電和去電的實(shí)現(xiàn)方法,涉及Android中BroadcastReceiver組件的使用及AndroidManifest.xml權(quán)限操作的相關(guān)技巧,需要的朋友可以參考下2016-08-08Android自定義dialog可選擇展示年月日時(shí)間選擇欄
這篇文章主要介紹了Android自定義dialog可選擇展示年月日時(shí)間選擇欄,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下2017-03-03Android自定義view利用Xfermode實(shí)現(xiàn)動(dòng)態(tài)文字加載動(dòng)畫(huà)
這篇文章主要介紹了Android自定義view利用Xfermode實(shí)現(xiàn)動(dòng)態(tài)文字加載動(dòng)畫(huà),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07