Modifier 点击(Clickable & CombinedClickable)


Modifier 点击(Clickable & CombinedClickable)

1. Clickable 点击

Clickable 修饰符用来监听组件的点击操作,并且当点击事件发生时会为被点击的组件施加一个波纹涟漪效果动画的蒙层。

fun Modifier.clickable(
  enabled: Boolean = true,
  onClickLabel: String? = null,
  role: Role? = null,
  onClick: () -> Unit
)
	
fun Modifier.clickable(
    interactionSource: MutableInteractionSource,
    indication: Indication?,
    enabled: Boolean,
    onClickLabel: String?,
    role: Role?,
    onClick: () -> Unit
)
  • enabled: 启用点击

  • onClickLabel: 无障碍提示文本

  • role: 无障碍提示控件的类型

  • indication: 默认为LocalIndication.current,设置为null禁用水波纹效果

  • interactionSource: 点击状态源

Box(modifier = Modifier
      .size(200.dp)
      .background(Color.Green)
      .clickable(enabled = enableState) {
        Log.d(TAG, "发生单击操作了~")
      }
  )

2. CombinedClickable 复合点击

对于长按点击、双击等复合类点击手势,我们可以使用 CombinedClickable 修饰符来实现手势监听。与 Clickable 修饰符一样,他同样也可以监听单击手势,并且也会为被点击的组件施加一个波纹涟漪效果动画的蒙层。

fun Modifier.combinedClickable(
    enabled: Boolean = true,
    onClickLabel: String? = null,
    role: Role? = null,
    onLongClickLabel: String? = null,
    onLongClick: (() -> Unit)? = null,
    onDoubleClick: (() -> Unit)? = null,
    onClick: () -> Unit
)

fun Modifier.combinedClickable(
    interactionSource: MutableInteractionSource,
    indication: Indication?,
    enabled: Boolean = true,
    onClickLabel: String? = null,
    role: Role? = null,
    onLongClickLabel: String? = null,
    onLongClick: (() -> Unit)? = null,
    onDoubleClick: (() -> Unit)? = null,
    onClick: () -> Unit
)
  • onLongClick: 长按点击
  • onDoubleClick: 双击
  • onClick: 点击
Box(modifier = Modifier
    .size(200.dp)
    .background(Color.Green)
    .combinedClickable(
      enabled = enableState,
      onLongClick = {
        Log.d(TAG, "发生长按点击操作了~")
      },
      onDoubleClick = {
        Log.d(TAG, "发生双击操作了~")
      },
      onClick = {
        Log.d(TAG, "发生单击操作了~")
      }
    )
  )

3. 不同点击状态下的按钮状态

创建 Data class 来记录不同状态下按钮的样式

data class ButtonState(var text: String, var textColor: Color, var buttonColor: Color)

使用 MutableInteractionSource() 获取状态,根据不同状态创建样式

  • collectIsPressedAsState(): 按下

  • collectIsHoveredAsState(): 鼠标移动到上方

  • collectIsFocusedAsState(): 获取到焦点

  • collectIsDraggedAsState(): 拖动

// 获取按钮的状态
val interactionState = remember { MutableInteractionSource() }

// 使用 Kotlin 的解构方法
val (text, textColor, buttonColor) = when {
    interactionState.collectIsPressedAsState().value  -> ButtonState("Just Pressed", Color.Red, Color.Black)
    else -> ButtonState( "Just Button", Color.White, Color.Red)
}

Button(
        onClick = { /*TODO*/ },
        interactionSource = interactionState,
        elevation = null,
        shape = RoundedCornerShape(50),
        colors = ButtonDefaults.buttonColors(
            backgroundColor = buttonColor,
        ),
        modifier = Modifier.width(IntrinsicSize.Min).height(IntrinsicSize.Min)
    ) {
        Text(
            text = text, color = textColor
        )
    }

4. 如何禁用涟漪(水波纹)效果

4.1 可以将indication设为null

modifier = Modifier
            .clickable(onClick = {}, indication = null, interactionSource = remember {
                MutableInteractionSource()
            })

4.2 使用pointerInput设置点击事件

modifier = Modifier.pointerInput(Unit) {
                    detectTapGestures(
                    // 长按事件
                    onLongPress = {}, 
                    // 点击事件
                    onTap = { })
                }

4.3 创建自定义 MutableInteractionSource 类

此方法可用于Button/Switch等控件

class NoRippleInteractionSource : MutableInteractionSource {
    override val interactions: Flow<Interaction> = emptyFlow()
    override suspend fun emit(interaction: Interaction) {}    
    override fun tryEmit(interaction: Interaction) = true
}

interactionSource = NoRippleInteractionSource()

4.4 创建自定义无波纹主题

private object NoRippleTheme : RippleTheme {
    @Composable
    override fun defaultColor() = Color.Unspecified

    @Composable
    override fun rippleAlpha(): RippleAlpha = RippleAlpha(0.0f,0.0f,0.0f,0.0f)
}

文章作者: Vinx
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Vinx !
 上一篇
Modifier 滑动(Swipeable) Modifier 滑动(Swipeable)
Modifier 滑动(Swipeable) swipeable 修饰符允许开发者通过锚点设置从而实现组件呈现吸附效果的动画,常用于开关等动画,也可用于下拉刷新等特殊效果的实现。 swipeable 修饰符不会为被修饰的组件提供任何
下一篇 
Icon加载资源图片显示黑色 Icon加载资源图片显示黑色
Icon加载资源图片显示黑色 Painter: //获取图片资源,R.drawable.xx或者R.mipmap.xx Icon(painter = painterResource(id = R.mipmap.head_icon), n
  目录