简单示例

@Composablefun Button(    text: String,    modifier: Modifier = Modifier.None,    onClick: (() -> Unit)? = null,    style: ButtonStyle = ContainedButtonStyle()) {    Button(modifier = modifier, style = style, onClick = onClick) {        Text(text = text)    }}

例如:

@Preview@Composablefun buttonLayout() {    Button(text = "点击",        onClick = {            // 处理点击事件        }    )}

或者传入可组合children来代替text属性

@Composablefun Button(    modifier: Modifier = Modifier.None,    onClick: (() -> Unit)? = null,    style: ButtonStyle = ContainedButtonStyle(),    children: @Composable() () -> Unit)

例如:

@Preview@Composablefun btLayout(){    Button(        onClick = {            // 处理点击事件        },children = {            Text(text = "点击")        }    )}

按钮样式

  • ContainedButtonStyle 默认样式
  • TextButtonStyle 文本样式
  • OutlinedButtonStyle 轮廓样式

Android Jetpack Compose - Button_第1张图片

@Preview@Composablefun btLayout() {   MaterialTheme {       Column(       ) {           TopAppBar(title = { Text("Compose演示") })           val (text, setText) = state {               "点击"           }           Container(               width = Dp(200.0f),               height = Dp(40.0f)           ) {               Button(                   onClick = {                       // 处理点击事件x                       setText("TextButtonStyle")                       //文字按钮                   }, children = {                       Text(text = text)                   }, style = TextButtonStyle(contentColor = Color.Magenta)               )           }           Container(               width = Dp(200.0f),               height = Dp(40.0f)           ) {               Button(                   onClick = {                   }, children = {                       Text(text = "ContainedButtonStyle")                   }, style = ContainedButtonStyle(                       backgroundColor = Color.Cyan,                       contentColor = Color.DarkGray,                       // 圆角                       shape =  RoundedCornerShape(8.dp),                       elevation = Dp(4f)                   )               )           }           Container(               width = Dp(200.0f),               height = Dp(40.0f)           ) {               Button(                   onClick = {                   }, children = {                       Text(text = "ContainedButtonStyle")                   }, style = OutlinedButtonStyle(                       backgroundColor = Color.Black,                       contentColor = Color.Cyan,                      // 边框颜色                       borderBrush = SolidColor(Color.Red),                       // 边款宽度                       borderWidth = Dp(4f),                       elevation = Dp(4f)                   )               )           }       }   }}

更多相关文章

  1. android之使用RxJava实现EventBus事件总线
  2. Android开发基础————通过内部类实现按钮点击事件
  3. android 滚动样式性别选择器简单实现
  4. android 处理双击事件
  5. Android事件传递流程
  6. android 返回键 事件
  7. Dialog 样式
  8. Android下的Touch事件分发

随机推荐

  1. Android(安卓)O WiFi启动流程
  2. Android动态加载dex技术初探
  3. android 实现天气预报(带动画的和不带动
  4. Android(安卓)Dial处理过程
  5. Android(安卓)Thread 常用方法
  6. Android(安卓)recovery 下使用 updater-s
  7. Android适用于IM通知音频的SoundPool
  8. android实现图片按任意路径截取
  9. 系出名门Android(7) - 控件(View)之ZoomC
  10. android src: eclipse 导入 android Sett