Android开发:Jetpack Compose Button,IconButton等各种Button的讲解

本文详细介绍了Android开发中Jetpack Compose的多种按钮使用,包括FloatingActionButton、ExtendedFloatingActionButton、IconToggleButton、RadioButton和TextButton。通过示例展示了各按钮的构造方法、属性配置,如形状、颜色、阴影、点击回调等,并提供了代码实现,帮助开发者更好地理解和应用这些组件。
摘要由CSDN通过智能技术生成

contentColor: Color = contentColorFor(backgroundColor),

elevation: FloatingActionButtonElevation = FloatingActionButtonDefaults.elevation(),

content: @Composable () -> Unit

){

}

  • onClick 点击回调

  • modifier 修饰符

  • interactionSource 跟上面一样,处理不同状态的

  • shape 形状

  • backgroundColor 背景颜色

  • contentColor内容颜色

  • elevation阴影

  • content 内容控件(其实ExtendedFloatingActionButton就是在该内容里添加了一个Row,Row里放了个文本跟Icon)

@Preview()

@Composable

fun floatingActionButtonTest(){

val context = LocalContext.current

val interactionSource = remember {

MutableInteractionSource()

}

val pressState = interactionSource.collectIsPressedAsState()

val pressText = if(pressState.value) “减少” else “添加”

Column(modifier = Modifier.padding(10.dp,10.dp)) {

FloatingActionButton(

onClick = {

Toast.makeText(context,“点击了按钮”,Toast.LENGTH_SHORT).show()

},

modifier = Modifier.size(80.dp),

interactionSource = interactionSource,

shape = RoundedCornerShape(25.dp),

backgroundColor = Color.Green,

contentColor = Color.Blue,

elevation = FloatingActionButtonDefaults.elevation(defaultElevation=8.dp,pressedElevation = 10.dp)

){

Row(verticalAlignment=Alignment.CenterVertically) {

Icon(imageVector = Icons.Filled.Add, contentDescription = “Add”,tint = Color.Red)

Spacer(modifier = Modifier.width(10.dp))

Text(text = “按钮”,fontSize =12.sp,color = Color.White)

}

}

}

ExtendedFloatingActionButton是FloatingActionButton的扩展 ExtendedFloatingActionButton的构造方法如下

@Composable

fun ExtendedFloatingActionButton(

text: @Composable () -> Unit,

onClick: () -> Unit,

modifier: Modifier = Modifier,

icon: @Composable (() -> Unit)? = null,

interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },

shape: Shape = MaterialTheme.shapes.small.copy(CornerSize(percent = 50)),

backgroundColor: Color = MaterialTheme.colors.secondary,

contentColor: Color = contentColorFor(backgroundColor),

elevation: FloatingActionButtonElevation = FloatingActionButtonDefaults.elevation()

) {

}

  • text显示的文本控件

  • onClick 点击回调

  • modifier 修饰符

  • icon 显示的icon控件

  • interactio

  • 19
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值