Facebook
From Me, 2 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 59
  1. fun TextfieldCustom() {
  2.     var text by remember { mutableStateOf(("")) }
  3.     var hint by remember { mutableStateOf(("")) }
  4.     var isClicked by remember { mutableStateOf((false)) }
  5.  
  6.     Column(
  7.         verticalArrangement = Arrangement.Center,
  8.         horizontalAlignment = Alignment.CenterHorizontally
  9.     ) {
  10.         Box(modifier = Modifier.height(20.dp).align(Alignment.Start)) {
  11.             [email protected](
  12.                 visible = isClicked,
  13.             ) {
  14.                 Text(text = "Hint")
  15.             }
  16.         }
  17.         TextField(
  18.             value = hint,
  19.             onValueChange = {
  20.                 text = it
  21.                 hint = if (it.isNotEmpty()) {
  22.                     text
  23.                 } else {
  24.                     it
  25.                 }
  26.             },
  27.             colors = TextFieldDefaults.textFieldColors(
  28.                 textColor = Color.Gray,
  29.                 disabledTextColor = Color.Transparent,
  30.                 backgroundColor = Color.Transparent,
  31.                 focusedIndicatorColor = Color.Transparent,
  32.                 unfocusedIndicatorColor = Color.Transparent,
  33.                 disabledIndicatorColor = Color.Transparent
  34.             ),
  35.  
  36.             modifier = Modifier
  37.                 .onFocusChanged {
  38.                     isClicked = it.isFocused
  39.                 }
  40.                 .background(Color.White)
  41.                 .drawBehind {
  42.                     val borderSize = 4.dp.toPx()
  43.                     drawLine(
  44.                         color = Color.Red,
  45.                         start = Offset(0f, size.height),
  46.                         end = Offset(size.width, size.height),
  47.                         strokeWidth = borderSize
  48.                     )
  49.  
  50.                 },
  51.             placeholder = {
  52.                 if (!isClicked) {
  53.                     Text(text = "Hint")
  54.                 } else {
  55.                     Text(text = "")
  56.                 }
  57.             },
  58.         )
  59.     }
  60. }}
  61.