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

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控件

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

  • shape 形状

  • backgroundColor 背景颜色

  • contentColor内容颜色

  • elevation阴影 通过FloatingActionButtonDefaults.elevation(defaultElevation,pressedElevation)设置,第一个参数是默认阴影,第二个是按下时候的阴影

@Preview()

@Composable

fun extendedFloatingActionButtonTest(){

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

ExtendedFloatingActionButton(text = { Text(text = pressText,fontSize =12.sp,color = Color.White)},onClick = {

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

},

// modifier = Modifier.size(50.dp),

icon ={

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

},

interactionSource = interactionSource,

shape = RoundedCornerShape(25.dp),

backgroundColor = Color.Green,

contentColor = Color.Blue,

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

}

}

四:IconToggleButton的用法

IconToggleButton是属于那种用于点赞,收藏的那种Icon可以改变状态的控件。代码如下:

@Composable

fun IconToggleButton(

checked: Boolean,

onCheckedChange: (Boolean) -> Unit,

modifier: Modifier = Modifier,

enabled: Boolean = true,

interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },

content: @Composable () -> Unit

) {

}

  • checked 是否选中的状态

  • onCheckedChange 是否选中状态变化的监听

  • modifier 修饰符

  • enabled 是否可用

  • interactionSource 跟上面Button解释一致

  • content 控件内容

比如我们举个例子,在点赞按下或者选中的时候,用红色的点赞,否则用黑色的点赞图标

@Preview()

@Composable

fun iconToggleButtonTest(){

val context = LocalContext.current

val interactionSource = remember {

MutableInteractionSource()

}

val pressState = interactionSource.collectIsPressedAsState()

var isCheck = remember {

mutableStateOf(false)

}

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

IconToggleButton(

checked = isCheck.value,

onCheckedChange = {

isCheck.value = it

},

modifier = Modifier.size(50.dp),

enabled = true,

interactionSource = interactionSource

){

Icon(imageVector = Icons.Filled.Favorite, contentDescription = “点赞图标”,tint = if(isCheck.value || pressState.value) Color.Red else Color.Black)

}

}

}

五:RadioButton的用法

RadioButton是单选按钮,RadioButton的代码如下

@Composable

fun RadioButton(

selected: Boolean,

onClick: (() -> Unit)?,

modifier: Modifier = Modifier,

enabled: Boolean = true,

interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },

colors: RadioButtonColors = RadioButtonDefaults.colors()

){

}

  • selected 是否选中

  • onClick 点击回调

  • modifier 修饰符

  • enabled 是否可用

  • interactionSource 跟上面Button解释一致

  • colors 设置颜色 RadioButtonDefaults.colors(selectedColor, unselectedColor,disabledColor)方法有三个参数,第一个selectedColor表示选中时候的颜色,第二个参数unselectedColor表示没有选中时候的颜色,第三个disabledColor表示不可用时候的颜色。

@Preview()

@Composable

fun iconToggleButtonTest(){

val context = LocalContext.current

val interactionSource = remember {

MutableInteractionSource()

}

val pressState = interactionSource.collectIsPressedAsState()

var isCheck = remember {

mutableStateOf(false)

}

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

RadioButton(

selected = isCheck.value,

onClick = {

isCheck.value = !isCheck.value

},

modifier = Modifier.size(50.dp),

enabled = true,

interactionSource = interactionSource,

colors = RadioButtonDefaults.colors(selectedColor = Color.Red,unselectedColor = Color.Black,disabledColor = Color.Gray))

}

}

六:TextButton的用法

TextButton文本按钮,来看看TextButton的代码,其实TextButton就是new了Button。

@Composable

fun TextButton(

onClick: () -> Unit,

modifier: Modifier = Modifier,

enabled: Boolean = true,

interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },

elevation: ButtonElevation? = null,

shape: Shape = MaterialTheme.shapes.small,

border: BorderStroke? = null,

colors: ButtonColors = ButtonDefaults.textButtonColors(),

contentPadding: PaddingValues = ButtonDefaults.TextButtonContentPadding,

content: @Composable RowScope.() -> Unit

) = Button(

onClick = onClick,

modifier = modifier,

enabled = enabled,

interactionSource = interactionSource,

elevation = elevation,

shape = shape,

border = border,

colors = colors,

contentPadding = contentPadding,

content = content

)

  • onClick 点击事件

  • modifier 修饰符

  • enabled 是否可用

  • interactionSource 跟Button一致

  • elevation 阴影

  • shape 形状设置

  • border 边框设置

  • colors 颜色设置

  • contentPadding 内容边距的设置

  • content 包含的控件

@Preview()

@Composable

fun textButtonTest(){

val context = LocalContext.current

val interactionSource = remember {

MutableInteractionSource()

}

val pressState = interactionSource.collectIsPressedAsState()

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

TextButton(

onClick = {

Toast.makeText(context,“点击了登录”,Toast.LENGTH_SHORT).show()

},

modifier = Modifier

.size(80.dp, 40.dp)

.clip(RoundedCornerShape(20)),

enabled = true,

interactionSource = interactionSource,

elevation = ButtonDefaults.elevation(2.dp,8.dp,0.dp),

shape = RoundedCornerShape(20),

border = BorderStroke(1.dp, if(pressState.value) Color.Red else Color.Yellow),

colors = ButtonDefaults.buttonColors(backgroundColor = Color.Blue,contentColor = Color.White,disabledBackgroundColor = Color.Gray,disabledContentColor = Color.Black),

contentPadding = ButtonDefaults.ContentPadding) {
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

小福利:

在当下这个碎片化信息环境的时代,很多资源都可以在网络上找到,只取决于你愿不愿意找或是找的方法对不对了

很多朋友不是没有资料,大多都是有几十上百个G,但是杂乱无章,不知道怎么看从哪看起,甚至是看后就忘

如果大家觉得自己在网上找的资料非常杂乱、不成体系的话,我也分享一套给大家,比较系统,我平常自己也会经常研读。

2021大厂最新Android面试真题解析

Android大厂面试真题解析

各个模块学习视频:如数据结构与算法

算法与数据结构资料图

只有系统,有方向的学习,才能在段时间内迅速提高自己的技术。
一线互联网架构师

这份体系学习笔记,适应人群:**第一,**学习知识比较碎片化,没有合理的学习路线与进阶方向。**第二,**开发几年,不知道如何进阶更进一步,比较迷茫。第三,到了合适的年纪,后续不知道该如何发展,转型管理,还是加强技术研究。如果你有需要,我这里恰好有为什么,不来领取!说不定能改变你现在的状态呢!点赞+评论即可获得!

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门即可获取!

src=“https://img-blog.csdnimg.cn/img_convert/b0c3f56c6ffb7384ce971fc5f7966bea.jpeg” />

小福利:

在当下这个碎片化信息环境的时代,很多资源都可以在网络上找到,只取决于你愿不愿意找或是找的方法对不对了

很多朋友不是没有资料,大多都是有几十上百个G,但是杂乱无章,不知道怎么看从哪看起,甚至是看后就忘

如果大家觉得自己在网上找的资料非常杂乱、不成体系的话,我也分享一套给大家,比较系统,我平常自己也会经常研读。

2021大厂最新Android面试真题解析

[外链图片转存中…(img-ogNwDcYU-1711791111535)]

各个模块学习视频:如数据结构与算法

[外链图片转存中…(img-r3tlRGxJ-1711791111535)]

只有系统,有方向的学习,才能在段时间内迅速提高自己的技术。
[外链图片转存中…(img-n5fD2rxf-1711791111535)]

[外链图片转存中…(img-MTfwEefg-1711791111536)]

这份体系学习笔记,适应人群:**第一,**学习知识比较碎片化,没有合理的学习路线与进阶方向。**第二,**开发几年,不知道如何进阶更进一步,比较迷茫。第三,到了合适的年纪,后续不知道该如何发展,转型管理,还是加强技术研究。如果你有需要,我这里恰好有为什么,不来领取!说不定能改变你现在的状态呢!点赞+评论即可获得!

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门即可获取!
  • 13
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当用户点击或长按按钮时,您可以使用 Jetpack Compose 的Clickable 组件。您可以将ClickEvent 和 LongPressEvent 传递给此组件,并在其中定义相应的操作。以下是示例代码: ``` import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.material.Button import androidx.compose.material.Text import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.unit.dp @Composable fun ButtonWithLongClick() { val buttonState = remember { mutableStateOf(0) } Column( horizontalAlignment = Alignment.CenterHorizontally, modifier = Modifier.fillMaxWidth() ) { Button( onClick = { buttonState.value = buttonState.value + 1 }, modifier = Modifier.clickable( onClick = { //add onClick event }, onLongClick = { //add onLongClick event } ), colors = ButtonDefaults.buttonColors(backgroundColor = Color.Gray) ) { Text( text = "Click & Long Click" ) } Text( text = "Button clicked " + buttonState.value + " times" ) } } ``` 在上面的示例代码中,使用了 Jetpack Compose 的 Column、Button 和 Text 组件,以及 clickable Modifier。使用 remember 可以创建可变状态,在 Button 中使用 onClick 和 onLongClick 传递 ClickEvent 和 LongPressEvent 以执行相应的操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值