简体   繁体   English

如何在 OnPress 上更改此圆形按钮的边框颜色?

[英]How to change border color of this circular Button on OnPress?

If I press on any of these Button it's border color should change and rest of should have no color.如果我按下这些按钮中的任何一个,它的边框颜色应该改变,其余的应该没有颜色。 Every time i press diffrent button only its border color should change and all other buttons should have no color.每次我按下不同的按钮时,只有它的边框颜色应该改变,所有其他按钮都应该没有颜色。 Here is a Image of what I want:-这是我想要的图像:- 在此处输入图片说明

<View
          style={{
            flexDirection: 'row',
            justifyContent: 'space-evenly',
          }}>
          <TouchableOpacity style={styles.Circlebtn}>
            <Icon.MaterialCommunityIcons name="calendar-today" size={24} />
            <Text>Day to day</Text>
          </TouchableOpacity>
          <TouchableOpacity style={styles.Circlebtn}>
            <Icon.MaterialCommunityIcons name="alarm-light" size={24} />
            <Text>Emergency</Text>
          </TouchableOpacity>
        </View>
        <View style={styles.mainview}>
          <TouchableOpacity style={styles.Circlebtn}>
            <Icon.MaterialCommunityIcons name="hammer-wrench" size={24} />
            <Text>Planned Works</Text>
          </TouchableOpacity>
          <TouchableOpacity style={styles.Circlebtn}>
            <Icon.Foundation name="clipboard-pencil" size={24} />
            <Text>Survey</Text>
          </TouchableOpacity>
        </View>
        <View style={styles.mainview}>
          <TouchableOpacity style={styles.Circlebtn}>
            <Icon.FontAwesome name="users" size={24} />
            <Text>Meeting</Text>
          </TouchableOpacity>
          <TouchableOpacity style={styles.Circlebtn}>
            <Icon.Entypo name="dots-three-horizontal" size={24} />
            <Text>Others</Text>
          </TouchableOpacity>
        </View>

You can add a variable to your state which holds your pressed button.您可以在状态中添加一个变量来保存您按下的按钮。 Let's say让我们说

index = 0

After that, each button click, you can update the state.之后,每点击一次按钮,就可以更新状态。 Let's say让我们说

const [index, setIndex] = useState(0);

<Button onClick={() => setIndex(1)} />
<AnotherButton onClick={() => setIndex(2)} />

If you check the style of each button according to this index variable, you can add your border to your button.如果您根据此index变量检查每个按钮的样式,则可以为按钮添加边框。

<Button style={{ borderColor: index === 1 ? 'green' : 'black' }} />
<AnotherButton style={{ borderColor: index === 2 ? 'green' : 'black' }} />

You can try these steps to put a border to your buttons.您可以尝试这些步骤为按钮添加边框。 I hope it works well我希望它运作良好

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

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