谁能帮帮我。这是我的源代码:https://snack.expo.io/rJFgyPDpH
想法是,如果我点击“1按钮”,它应该是“红色”,如果我点击**“2按钮”也应该将其颜色更改为“红色”,但“1按钮”应该更改为默认颜色,即黑色。但是,“2按钮”**。
如果我的方法太简单,也欢迎其他方法(如TouchableHighlight
,ES6等)。我很感激你能指出我的错误,让我从中吸取教训。
谁能帮帮我。这是我的源代码:https://snack.expo.io/rJFgyPDpH
想法是,如果我点击“1按钮”,它应该是“红色”,如果我点击**“2按钮”也应该将其颜色更改为“红色”,但“1按钮”应该更改为默认颜色,即黑色。但是,“2按钮”**。
如果我的方法太简单,也欢迎其他方法(如TouchableHighlight
,ES6等)。我很感激你能指出我的错误,让我从中吸取教训。
5条答案
按热度按时间gjmwrych1#
试试下面
b1uwtaje2#
你可以这样写代码:
现在,如果你点击第一个按钮,它应该是“红色”,但第二,仍然是“黑色”的背景。因此,如果你点击第二个按钮,它应该是“红色”,而第一个按钮应该是“黑色”。
xoshrz7s3#
根据您的要求,按下第一个按钮,它将调用changeColor。按下第二个按钮,它将调用changeColor2。
在代码中,onPress第二个按钮,它可以改变为changeColor2而不是changeColor函数。
这
而不是
gstyhher4#
通过传递id,你可以改变颜色
Live demo
1l5u6lss5#
因此,所有当前的方法都使用onPress来管理状态。
如果您在以下位置查看文档:https://reactnative.dev/docs/touchableopacity
TouchOpacity组件继承了https://reactnative.dev/docs/touchablewithoutfeedback#props的所有属性
这意味着你可以访问onPressIn和onPressOut,我个人使用它们来管理状态,而不是onPress。
这是我可能过于复杂的例子,我也使用了阴影和渐变: