如何做网签合同 网站,500个公司取名大全,软件开发电脑推荐,公司网站备案多少钱文章目录前言一、Button的基本使用1.Button2.扩展TextButtonOutlinedButtonIconButton二、Button属性前言
Button是Compose中作为按钮提供交互的控件#xff0c;它本身属性自带onClick的点击处理#xff0c;不需要使用Modifiter中的点击功能#xff0c;目前最新版默认按钮风…文章目录前言一、Button的基本使用1.Button2.扩展TextButtonOutlinedButtonIconButton二、Button属性前言Button是Compose中作为按钮提供交互的控件它本身属性自带onClick的点击处理不需要使用Modifiter中的点击功能目前最新版默认按钮风格为Material风格的圆角所以需要手动调节为矩形或更改圆角半径。按钮的官方中文文档参考https://developer.android.com/reference/kotlin/androidx/compose/material/package-summary#button一、Button的基本使用1.Button最常用的按钮控件本身自带onClick,不需要Modifiter的点击事件如果两者同时设置时按钮自身的优先级更高内容中设置的文字图形等会按照横向布局排列注意设置颜色时容器的上色与内容的上色有所不同下图为两种模式下各自设为红色的表现。Button(//默认为material风格的圆角矩形onClick{//设置点击事件时做出的反应Log.i(MainActivity,ButtonSample: 按钮)},//可以按钮启用与未启用时的容器、内容等颜色colorsButtonDefaults.buttonColors(contentColorColor.Red),//展示内容的颜色modifierModifier.padding(10.dp),//内边距shapeRectangleShape//矩形也可以设置为0dp的圆角RoundedCornerShape(0.dp){//当前作用域内为横向布局呈现按钮中展示内容Icon(Icons.Filled.Favorite,//Material库中的图标contentDescription实心爱心图案,//无障碍描述modifierModifier.size(ButtonDefaults.IconSize)//设置尺寸为默认按钮尺寸)Text(内容1)}下图为下面三种按钮的变体所展示的效果2.扩展TextButton相当于带有点击事件的文本系统封装了Text将点击事件添加进去可以更方便给text设置点击事件不再需要用Modifiter的点击功能。TextButton(//带有点击事件的文本。等同于封装了Text将点击事件功能添加上因此不需要modifier的点击功能onClick{}//点击事件逻辑){Text(封装了点击功能的文本)}OutlinedButton显示效果为带有主题颜色的边框透明背景的按钮当然也可以自己设置来改变这种效果它的本意是用于次要操作的按钮以便于跟主要按钮Button区分避免一个页面按钮过多分散用户注意力因此可以一个页面设置一个Button其他用OutlinedButton来提供更好的体验OutlinedButton(//带有主题颜色的边框透明背景的按钮,常用于次要操作按钮如一个页面中设置一个Button其他都OutLinedButton作为次要操作onClick{}//点击事件逻辑){Text(低视觉权重按钮)}IconButton用于展示图标的按钮属于官方封装的便捷按钮本质是通过Box布局进行的元素堆叠因此设置多个元素会重叠在一起。IconButton(//图标按钮 添加图标与文字后会重叠在一起enabledfalse,//是否设置禁用onClick{//点击事件逻辑}){//以下内容会重叠在一起通常只设置一个图案Icon(Icons.Filled.Favorite,//Material库中的图标contentDescription实心爱心图案,//无障碍描述modifierModifier.size(ButtonDefaults.IconSize)//设置尺寸为默认按钮尺寸)Icon(Icons.Filled.AccountBox,//Material库中的图标contentDescription实心爱心图案,//无障碍描述modifierModifier.size(ButtonDefaults.IconSize)//设置尺寸为默认按钮尺寸)Text(内容2)}二、Button属性ComposablefunCustomButtonDemo(){// 1. 状态管理监听按钮按下状态基于interactionSourcevalinteractionSourceremember{MutableInteractionSource()}//缓存 交互状态监听器valisPressedbyinteractionSource.collectIsPressedAsState()// 通过监听器获取当前按下状态valisEnabledremember{mutableStateOf(true)}// 控制按钮启用/禁用// 2. 自定义按钮颜色不同状态下的背景/内容色valbuttonColorsButtonDefaults.buttonColors(containerColorColor(0xFF6200EE),// 默认背景色紫色contentColorColor.White,// 默认内容色文字/图标disabledContainerColorColor.Gray.copy(0.5f),// 禁用时背景色disabledContentColorColor.White.copy(0.7f)// 禁用时内容色)// 3. 自定义边框1dp红色边框valcustomBorderBorderStroke(1.dp,Color.Red)// 4. 自定义阴影按下时增加阴影valcustomElevationButtonDefaults.buttonElevation(defaultElevation4.dp,// 默认阴影pressedElevation8.dp,// 按下时阴影disabledElevation0.dp// 禁用时无阴影)// 5. 按钮主体结合所有属性Button(onClick{// 点击后切换为禁用状态如果没有外部逻辑启用则不会再触发onclickisEnabled.value!isEnabled.valueprintln(按钮被点击当前启用状态${isEnabled.value})},modifierModifier.padding(horizontal16.dp,vertical8.dp)// 外部边距.width(200.dp),// 固定宽度enabledisEnabled.value,// 是否禁用通过上面绑定启用状态来决定也由点击事件所改变interactionSourceinteractionSource,// 绑定缓存的交互状态源感知上面设置的按下操作elevationcustomElevation,// 自定义阴影shapeRoundedCornerShape(12.dp),// 自定义圆角12dpbordercustomBorder,// 自定义边框colorsbuttonColors,// 自定义颜色contentPaddingPaddingValues(horizontal20.dp,vertical10.dp),// 自定义内边距content{// 按钮内部内容RowScope自动横向排列Icon(imageVectorIcons.Filled.Check,contentDescription确认,modifierModifier.size(ButtonDefaults.IconSize))// 图标与文字间距Spacer(modifierModifier.size(ButtonDefaults.IconSpacing))// 文字结合按下状态动态修改Text(textif(isPressed)按下中...elseif(isEnabled.value)点击切换禁用else已禁用,styleMaterialTheme.typography.labelLarge)})}