在 React Native ButtonGroup 中选择按钮
Selecting button in React Native ButtonGroup
我有个小问题。我使用的 ButtonGroup 以前使用两个选项,但现在我提供 4 个选项。
它在屏幕上正确呈现,但我只能 select 前两个选项。我如何在 select 离子之间跳转,以便我可以 select 不同的按钮。
欢迎任何帮助!
<ButtonGroup
selectedIndex={this.state.test === "First" ? 0 : 1}
buttons={["First", "Second", "Third", "Fourth"]}
containerStyle={{ height: 50 }}
selectedButtonStyle={{ backgroundColor: "red" }}
selectedTextStyle={{ color: "white" }}
textStyle={{ fontSize: 18, fontWeight: '600' }}
onPress={(selectedIndex) =>
this.setState({
test: buttons[selectedIndex]
})}
/>
发生这种情况是因为您设置了 selectedIndex={this.state.test === "First" ? 0 : 1}
,所以您只有 2 个索引(和 4 个按钮)。
您可以创建一个状态变量来管理索引。类似于:
this.state = {
...
selectedIndex: 0
}
...
<ButtonGroup
selectedIndex={this.state.selectedIndex}
buttons={["First", "Second", "Third", "Fourth"]}
containerStyle={{ height: 50 }}
selectedButtonStyle={{ backgroundColor: "red" }}
selectedTextStyle={{ color: "white" }}
textStyle={{ fontSize: 18, fontWeight: '600' }}
onPress={(selectedIndex) => {
this.setState({ test: buttons[selectedIndex] });
this.setState({ selectedIndex: selectedIndex });
}}
/>
Index可以保持状态,然后在onPress中赋值给selectedIndex,应该可以用于不同按钮的选择
this.state = {
test: "First",
btnIndex: 0
}
<ButtonGroup
selectedIndex={this.state.btnIndex}
buttons={["First", "Second", "Third", "Fourth"]}
containerStyle={{ height: 50 }}
selectedButtonStyle={{ backgroundColor: "red" }}
selectedTextStyle={{ color: "white" }}
textStyle={{ fontSize: 18, fontWeight: '600' }}
onPress={(selectedIndex) => {
this.setState({
test: buttons[selectedIndex],
btnIndex: selectedIndex
});
}}
/>
我有个小问题。我使用的 ButtonGroup 以前使用两个选项,但现在我提供 4 个选项。
它在屏幕上正确呈现,但我只能 select 前两个选项。我如何在 select 离子之间跳转,以便我可以 select 不同的按钮。
欢迎任何帮助!
<ButtonGroup
selectedIndex={this.state.test === "First" ? 0 : 1}
buttons={["First", "Second", "Third", "Fourth"]}
containerStyle={{ height: 50 }}
selectedButtonStyle={{ backgroundColor: "red" }}
selectedTextStyle={{ color: "white" }}
textStyle={{ fontSize: 18, fontWeight: '600' }}
onPress={(selectedIndex) =>
this.setState({
test: buttons[selectedIndex]
})}
/>
发生这种情况是因为您设置了 selectedIndex={this.state.test === "First" ? 0 : 1}
,所以您只有 2 个索引(和 4 个按钮)。
您可以创建一个状态变量来管理索引。类似于:
this.state = {
...
selectedIndex: 0
}
...
<ButtonGroup
selectedIndex={this.state.selectedIndex}
buttons={["First", "Second", "Third", "Fourth"]}
containerStyle={{ height: 50 }}
selectedButtonStyle={{ backgroundColor: "red" }}
selectedTextStyle={{ color: "white" }}
textStyle={{ fontSize: 18, fontWeight: '600' }}
onPress={(selectedIndex) => {
this.setState({ test: buttons[selectedIndex] });
this.setState({ selectedIndex: selectedIndex });
}}
/>
Index可以保持状态,然后在onPress中赋值给selectedIndex,应该可以用于不同按钮的选择
this.state = {
test: "First",
btnIndex: 0
}
<ButtonGroup
selectedIndex={this.state.btnIndex}
buttons={["First", "Second", "Third", "Fourth"]}
containerStyle={{ height: 50 }}
selectedButtonStyle={{ backgroundColor: "red" }}
selectedTextStyle={{ color: "white" }}
textStyle={{ fontSize: 18, fontWeight: '600' }}
onPress={(selectedIndex) => {
this.setState({
test: buttons[selectedIndex],
btnIndex: selectedIndex
});
}}
/>