[英]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
具有onClick
和onClickLabel
作为 API 的一部分。
And for composables without onClick
, we can use Modifier.clickable
or Modifier.semantics
.对于没有
onClick
的可组合项,我们可以使用Modifier.clickable
或Modifier.semantics
。
But what about Button
, IconButton
, FloatingActionButton
, TextButton
and OutlinedButton
?但是
Button
、 IconButton
、 FloatingActionButton
、 TextButton
和OutlinedButton
呢?
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.clickable
或Modifier.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
,因为:
onClick
lambda onClick
lambda 处理点击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.