首页 > 解决方案 > 选择选项后如何禁用反应本机选择器

问题描述

我想从下拉列表中选择一个选项后立即禁用(我不想选择任何内容)选择器。这里是选择器,

<View style={editProfileStyle.pickerView}>
            <Picker
              style={{
                ...Platform.select({
                  android: {
                    color: "#000"
                  }
                })
              }}
              enabled={true}
              selectedValue={this.state.Qualification}
              onValueChange={this.updateState}
              mode="dropdown"
              itemStyle={{ height: 80, color: "#000000" }}
            >
                {this.state.options.map((item, index) => {
                    return <Picker.Item value={item} key={index} label={item} />;
                })}
            </Picker>
          </View>

如果找到解决方案将有很大帮助!

标签: react-nativepicker

解决方案


您可以使用 enabled props , enabled ={this.state.enabled} ,当用户选择日期时,调用 setState 并将启用状态切换为 false。请记住,启用的道具仅适用于 android。这是一个例子:

constructor(props) {
    super(props);
    this.state = { 
        enabled: true // Enabled by default , you can change it
     };

// inside your updateState function call : this.setState({enabled:false})
// if you dont already defined updateState
// updateState = () =>{ this.setState({enabled:false})
<View style={editProfileStyle.pickerView}>
            <Picker
              style={{
                ...Platform.select({
                  android: {
                    color: "#000"
                  }
                })
              }}
              enabled={this.state.enabled}
              selectedValue={this.state.Qualification}
              onValueChange={this.updateState}
              mode="dropdown"
              itemStyle={{ height: 80, color: "#000000" }}
            >
                {this.state.options.map((item, index) => {
                    return <Picker.Item value={item} key={index} label={item} />;
                })}
            </Picker>
          </View>

推荐阅读