android - 如何在 android compose 中创建 OTP 布局?

标签 android android-jetpack-compose

我在新的 android jetpack compose 中为我的应用程序登录。
我想制作一个像给定照片中的 OTP 布局。 OTP Layout

最佳答案

otp 中每个字符的非常简单的布局。

   @Composable
   fun OtpChat(){
       var text by remember { mutableStateOf("1") }
       val maxChar = 1

       Column(Modifier.background(DarkGray),
              horizontalAlignment = Alignment.CenterHorizontally){
           TextField(
               value =text,
               onValueChange = {if (it.length <= maxChar) text = it},
               modifier = Modifier.width(50.dp),
               textStyle = LocalTextStyle.current.copy(
                   fontSize = 20.sp, 
                   textAlign= TextAlign.Center),
               colors= TextFieldDefaults.textFieldColors(
                   textColor = White,
                   backgroundColor = Transparent,
                   unfocusedIndicatorColor = Transparent,
                   focusedIndicatorColor = Transparent)
           )
           Divider(Modifier
                    .width(28.dp)
                    .padding(bottom = 2.dp)
                    .offset(y=-10.dp), 
             color = White,
             thickness = 1.dp)
       }
   }
enter image description here
然后只需使用 Row 之类的东西显示 6 OtpChar s
   Row(horizontalArrangement = Arrangement.SpaceBetween){
       OtpChar()
       OtpChar()
       OtpChar()
       //....
   }
enter image description here

关于android - 如何在 android compose 中创建 OTP 布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69270834/

相关文章:

android - deleteDirectory : java. lang.NoSuchMethodError: 没有虚拟方法 toPath

android - 初始相机位置 Google Maps Compose

java - 打开 GPS 时快速按下按钮后出现错误

android - 没有延迟更新 View 时没有错误 "Only the original thread that created a view hierarchy can touch its views"

material-design - 为 Compose 创建 Material Design 3 主题时如何处理形状

android - 使用jetpack compose时android studio gradle脚本中compileSdk和compileSdkVersion有什么区别

android - 如何减少jetpack compose `TopAppBar`中导航图标和标题之间的水平空间?

android - 找不到方法 '' com.amazon.ion.system.IonBinaryWriterBuilder"

java - 当在 CustomView 中扩展 ArrayAdapter 时,将 Kotlin 翻译为 Android 中的 Java

android - 如何访问库项目中自定义 FrameLayout 的子元素?