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

Text(text = stringResource(id = R.string.login))

}

}

}

二: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

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

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

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

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

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

如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
img

写在最后

最后我想说:对于程序员来说,要学习的知识内容、技术有太多太多,要想不被环境淘汰就只有不断提升自己,从来都是我们去适应环境,而不是环境来适应我们!

这里附上上述的技术体系图相关的几十套腾讯、头条、阿里、美团等公司2021年的面试题,把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,这里以图片的形式给大家展示一部分。

相信它会给大家带来很多收获:

当程序员容易,当一个优秀的程序员是需要不断学习的,从初级程序员到高级程序员,从初级架构师到资深架构师,或者走向管理,从技术经理到技术总监,每个阶段都需要掌握不同的能力。早早确定自己的职业方向,才能在工作和能力提升中甩开同龄人。

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
img
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新*

如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
[外链图片转存中…(img-RegX4b3R-1712776825623)]

写在最后

最后我想说:对于程序员来说,要学习的知识内容、技术有太多太多,要想不被环境淘汰就只有不断提升自己,从来都是我们去适应环境,而不是环境来适应我们!

这里附上上述的技术体系图相关的几十套腾讯、头条、阿里、美团等公司2021年的面试题,把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,这里以图片的形式给大家展示一部分。

相信它会给大家带来很多收获:

[外链图片转存中…(img-hDCLbiPy-1712776825623)]

[外链图片转存中…(img-pscFoJZC-1712776825624)]

当程序员容易,当一个优秀的程序员是需要不断学习的,从初级程序员到高级程序员,从初级架构师到资深架构师,或者走向管理,从技术经理到技术总监,每个阶段都需要掌握不同的能力。早早确定自己的职业方向,才能在工作和能力提升中甩开同龄人。

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
[外链图片转存中…(img-YBSHp6ln-1712776825624)]

  • 11
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值