• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Android Compose搜索框实现

武飞扬头像
wk灬丨
帮助2

学新通

  1.  
    class MainActivity : ComponentActivity() {
  2.  
     
  3.  
    override fun onCreate(savedInstanceState: Bundle?) {
  4.  
    super.onCreate(savedInstanceState)
  5.  
    setContent {
  6.  
    Column(Modifier.fillMaxSize()) {
  7.  
    Spacer(Modifier.height(50.dp))
  8.  
    searchBar()
  9.  
    }
  10.  
    }
  11.  
    }
  12.  
    }
  13.  
     
  14.  
    @Composable
  15.  
    fun searchBar() {
  16.  
    var text by remember { mutableStateOf("")}
  17.  
    Box(modifier = Modifier.background(Color(0xFFD3D3D3)).padding(50.dp)) {
  18.  
    BasicTextField(value = text, onValueChange = { text = it }, decorationBox = {
  19.  
    Row(verticalAlignment = Alignment.CenterVertically,
  20.  
    modifier = Modifier.padding(horizontal = 8.dp)) {
  21.  
    Icon(imageVector = Icons.Filled.Search, contentDescription = "")
  22.  
    Box(modifier = Modifier.padding(horizontal = 10.dp).weight(1f),
  23.  
    contentAlignment = Alignment.CenterStart) {
  24.  
    if (text.isEmpty()) {
  25.  
    Text(text = "输入点东西康康吧~" ,
  26.  
    style = TextStyle(Color(0,0,0,128))
  27.  
    )
  28.  
    }
  29.  
    it()
  30.  
    }
  31.  
    if (text.isNotEmpty()) {
  32.  
    IconButton(onClick = { text = ""}, modifier = Modifier.size(16.dp)) {
  33.  
    Icon(imageVector = Icons.Filled.Close, contentDescription = "")
  34.  
    }
  35.  
    }
  36.  
    }
  37.  
    }, modifier = Modifier.padding(10.dp).background(Color.White, CircleShape)
  38.  
    .height(30.dp).fillMaxWidth()
  39.  
    )
  40.  
    }
  41.  
    }
学新通

运行后的效果:

学新通

 这个搜索框用Compose实现起来看上去有点嵌套地狱,主要还是不熟悉控件里面的属性

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhigcfaj
系列文章
更多 icon
同类精品
更多 icon
继续加载