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

二:IconButton的用法

IconButton的构造方法如下

@Composable

fun IconButton(

onClick: () -> Unit,

modifier: Modifier = Modifier,

enabled: Boolean = true,

interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },

content: @Composable () -> Unit

){

}

  • onClick 点击回调

  • modifier 修饰符

  • enabled 是否可用

  • content 该控件包含的内容

  • interactionSource 跟Button一样,可以根据不同状态去处理一些逻辑。比如按下时候如何,没有按下时候如何。

如下举例。比如IconButton是一个左边是+的icon,右边是文本的控件。该文本如果按下时候是显示减少,不按下显示添加。

@Preview()

@Composable

fun iconButtonTest(){

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)) {

IconButton(

onClick = {

Toast.makeText(context,“点击了添加”,Toast.LENGTH_SHORT).show()

},

modifier = Modifier.size(80.dp,40.dp).clip(RoundedCornerShape(20)),

enabled = true,

interactionSource = interactionSource

){

Row(verticalAlignment=Alignment.CenterVertically) {

Icon(imageVector = Icons.Filled.Add, contentDescription = “添加的按钮”,tint = Color.Red)

Text(text = pressText,fontSize = 8.sp)

}

}

}

}

三:FloatingActionButton和ExtendedFloatingActionButton的用法

FloatingActionButton是Material风格的控件,默认是浮动在右下角的圆形控件。FloatingActionButton的构造函数方法如下:

@Composable

fun FloatingActionButton(

onClick: () -> Unit,

modifier: Modifier = Modifier,

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(),

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 “添加”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值