简体   繁体   English

Jetpack 为 Button 和 IconButton 组合 onClickLabel

[英]Jetpack compose onClickLabel for Button and IconButton

Working on accessibility enhancements for my app.致力于我的应用程序的可访问性增强。

From Docs , I can see that Card has both onClick and onClickLabel as part of the API.Docs中,我可以看到Card具有onClickonClickLabel作为 API 的一部分。

And for composables without onClick , we can use Modifier.clickable or Modifier.semantics .对于没有onClick的可组合项,我们可以使用Modifier.clickableModifier.semantics

But what about Button , IconButton , FloatingActionButton , TextButton and OutlinedButton ?但是ButtonIconButtonFloatingActionButtonTextButtonOutlinedButton呢?
Composables which have onClick as part of API but no onClickLabel .具有onClick作为 API 的一部分但没有onClickLabel的可组合项。

This works as expected.这按预期工作。 But is this the correct way to write this code?但这是编写这段代码的正确方法吗?

IconButton(
    onClick = navigateUp,
    modifier = Modifier.clickable(
        onClickLabel = "Navigate up",
        onClick = {},
    )
) {
    Icon(
        imageVector = Icons.Rounded.ArrowBack,
        contentDescription = stringResource(
            id = R.string.top_app_bar_content_description_navigate_up,
        ),
    )
}

This seems too confusing as IconButton expects onClick .这似乎太混乱了,因为IconButton期望onClick So I cannot go with completely Modifier.clickable or Modifier.semantics .所以我不能 go 完全Modifier.clickableModifier.semantics
Similarly Modifier.clickable also expects onClick .同样, Modifier.clickable也需要onClick

So, I have two onClick for a single composable component which could easily cause a lot of hard to debug bugs.因此,我有两个onClick用于单个可组合组件,这很容易导致很多难以调试的错误。

Reason for the change,改变的原因,
To read "Double tap to Navigate up" instead of the default "Double tap to activate".阅读“双击向上导航”而不是默认的“双击激活”。

PS:附言:
Compose version - 1.0.4撰写版本 - 1.0.4

You don't need the Modifier.clickable in this scenario because:在这种情况下您不需要Modifier.clickable ,因为:

  • the IconButton already handles clicks via its onClick lambda IconButton 已经通过其onClick lambda 处理点击
  • the Icon has a contentDescription field for accessibility Icon有一个用于辅助功能的contentDescription字段

So all you need is this所以你只需要这个

IconButton(
    onClick = navigateUp,
) {
    Icon(
        imageVector = Icons.Rounded.ArrowBack,
        contentDescription = stringResource(
            id = R.string.top_app_bar_content_description_navigate_up,
        ),
   )
}

It doesn't seem to be the prettiest solution but I ended up doing:它似乎不是最漂亮的解决方案,但我最终做了:

      IconButton(
        modifier = Modifier.size(40.dp).clearAndSetSemantics {
            this.onClick(label = closeLabelDescription, action = null)
            this.contentDescription = closeDescription
            this.role = Role.Button
        },
        onClick = onClickClose,
    ) {
        Icon(
            painter = painterResource(R.drawable.ic_close),
            tint = RiotWhite,
            contentDescription = null
        )
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM