如何更改从数组动态创建的 TouchableOpacity 的 backgroundColor?

拉德萨

我想更改 TouchableOpacity 的 backgroundColor 和 BorderColor。我知道我可以使用 state 来更改 TouchableOpacity BGColor onPress,但是 TouchableOpacity 是从 arrayList 动态创建的,我只想更改按下的 touchableOpacity 的 backgroundColor 和 BorderCorlor。

这是我创建按钮的代码:

 var sizeOptions = [];
 for (var i = 0; i < product.Items[0].Items.length; i++) {
   for (var j = 0; j <  product.Items[0].Items[i].SKUOptions.length; j++) {
     if (product.Items[0].Items[i].SKUOptions[j].Alias == 'Tamanho') {
       var sizeTitle = product.Items[0].Items[i].SKUOptions[j].Title;
       sizeOptions.push(
          <TouchableOpacity style={{marginLeft:20, height:40, width:40, borderRadius:1, borderWidth:1, borderStyle: 'solid', borderColor:'#000', backgroundColor:'#fff'}} key = {i} onPress={() => this.selectSize(sizeTitle)}>
            <Text style={[{marginTop:10, width:40, height:40, textAlign: 'center'}]}> {sizeTitle} </Text>
        </TouchableOpacity>
     )
     }
   }
 }

return (
  <ScrollView contentContainerStyle={{flexGrow: 1, justifyContent: 'center'}} horizontal={true} style={{marginTop:15, marginLeft: 15}}>
  { sizeOptions }
  </ScrollView>
);

此代码已经可以显示创建的按钮,我只需要在按下时更改颜色。

安德烈科瓦丘

state对象可以有一个叫做财产touchableOpacityIndexPressed这与初始化的-1开头。

constructor(props) {
  super(props);
  this.state = { touchableOpacityIndexPressed: -1 }
  ...
}

当touchable被推到数组时,你可以做到以下几点:

<TouchableOpacity style={{marginLeft:20, height:40, width:40, borderRadius:1, borderWidth:1, borderStyle: 'solid', borderColor: this.state.touchableOpacityIndexPressed === (i + j) ? 'red' : '#000', backgroundColor: this.state.touchableOpacityIndexPressed === (i + j) ? 'blue' :'#fff'}} key = {i} onPress={() => this.selectSize(sizeTitle, (i + j)}>

确保在selectSize方法的touchableOpacityIndexPressed属性中设置正确的状态,在我的示例中,我将其设置为(i + j).

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

动态更改CardView的BackgroundColor

如何禁用touchableOpacity?

从数组设置随机backgroundColor

如何通过jquery更改backgroundcolor?

如何从 ImageView 获取 backgroundColor

如何使touchableOpacity函数正常工作?

使用MVVM更改按钮BackgroundColor

斯威夫特:UICollectionViewCell didSelectItemAtIndexPath更改backgroundColor

全局更改导航器backgroundColor

如何修复backgroundColor分配错误?

如何更改BackgroundColor OnMouseHover C ++ MFC

如何使用OnPlatform Xamarin更改BackgroundColor

在 React Native 中更改 TouchableOpacity 的颜色

如何通过MapView区域更改来更改TouchableOpacity颜色?

如何在文本ReactNative中包含TouchableOpacity

如何禁用TouchableOpacity中的可点击开关?

如何在 TouchableOpacity 中旋转图像

动态 backgroundColor UIImageView 不更新 iOS

根据状态值更改backgroundColor

Xamarion.iOS statusBar BackgroundColor不会更改

Sprite Kit:使用ColorizeWithColor更改backgroundcolor

使用React Native更改backgroundColor BEHIND键盘?

UICollectionviewCell backgroundColor选中后不更改

通过JavaScript更改ASP:TEXTBOX的BackgroundColor

使用* ngFor [ngStyle]更改所有backgroundColor

React-Native-如何为TouchableOpacity组件创建禁用的样式?

如何在 TouchableOpacity 标签中嵌入的按钮标签中触发 onPress 而不在 TouchableOpacity 中触发 onPress

如何绑定WPF listview项目的backgroundcolor?

如何以编程方式设置WKInterfaceController的backgroundColor?