Android Compose Custom TextField how to implement Custom input Box
This article mainly introduces Android Compose custom TextField how to achieve a custom input box, the article is very detailed, has a certain reference value, interested friends must read it!
Simple custom BasicTextField example
1. Code
Var text by remember {mutableStateOf ("simple TextField")} BasicTextField (value = text, onValueChange = {text = it}, modifier = Modifier .height (40.dp) .width (300.dp) .background (Color.Green))
two。 Effect.
BasicTextField that implements custom styles
We know that BasicTextField provides a basic input box, including only text input, cursor and other simple functions, if we need to add styles, we need to implement our own decorationBox parameters to add styles.
In this example, we implement a custom BasicTextField with a blue border, filled with green inside, and an icon on the left.
1. Code
@ 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 is responsible for writing input box style 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 () / / Custom style is the key It cannot be displayed without this line of input text, and the cursor cannot see})}}
two。 Effect.
Use BasicTextField to customize Baidu input box
1. Code
@ Composablefun BaiduTextField () {var text by remember {mutableStateOf ("Android")} 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 = "Baidu") Color = Color.White)}})}
two。 Effect.
The above is all the content of the article "how to realize the custom input box of Android Compose custom TextField". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!