【问题标题】:Handle scroll up event for button on focus of Text-Input in react-native在 react-native 中处理文本输入焦点上按钮的向上滚动事件
【发布时间】:2018-03-21 23:26:48
【问题描述】:

我正在使用 react-native。 在我的情况下,当我的文本输入聚焦时,键盘弹出窗口会隐藏我的视图,其中的按钮存在。

我的代码 sn-p:

 <KeyboardAvoid>
          <View style={{flexDirection:'row',width:dwidth-20,marginHorizontal:20,marginTop:15}}>
            <TextInput 
            style={{width:width-60,fontWeight:'bold',fontSize:18,}}
            placeholder={this.state.quesText}
            autoCapitalize="words"
            onKeyPress={this.typing}
            value={this.state.questionText}
            onChangeText={ (questionText) => this.setState({questionText})}
            />
          </View>
          </KeyboardAvoid>
        </View>

        <View style={{borderBottomWidth: 0.5,paddingVertical:1,borderBottomColor: '#fff',}}/>

        <View style ={{flexDirection:'row',height:dheight/15,backgroundColor:'#fff',paddingLeft:18}}>
          <CheckBox style={{padding:5,marginBottom:2}} onClick={ () => this.setState({ showUserImg: !this.state.showUserImg,textValue: !this.state.textValue,anonymFlage:"Y"}) } checkBoxColor="#2b73e1"/>
          <Text style={{marginTop:6,fontSize:16,marginLeft:5,}}>
          {this.state.askAnonText}
          </Text>
          <View style={styles.button}>
            <TouchableOpacity activeOpacity={.5} onPress={(e) => this.formValidation()}>
             <Text style={styles.buttonText}>{Post}</Text>
            </TouchableOpacity>
          </View>
        </View>

我不想在键盘出现时隐藏“发布”按钮。当键盘出现时,如何在点击 textinput 时上移我的 Post 按钮。有什么解决办法?

【问题讨论】:

    标签: react-native


    【解决方案1】:

    最简单、最容易安装的解决方案是KeyboardAvoidingView。它是一个核心组件,但它的作用也很简单。这将使您的视图更自然地与键盘一起流动。

    【讨论】:

      【解决方案2】:
      import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view';
      .
      .
      .
      
      <KeyboardAwareScrollView>
          <View style={{flexDirection:'row',width:dwidth-20,marginHorizontal:20,marginTop:15}}>
            <TextInput  style={{width:width-60,fontWeight:'bold',fontSize:18,}}
                        placeholder={this.state.quesText}
                        autoCapitalize="words"
                        onKeyPress={this.typing}
                        value={this.state.questionText}
                        onChangeText={ (questionText) => this.setState({questionText})}
            />
          </View>
          <View style={{borderBottomWidth: 0.5,paddingVertical:1,borderBottomColor: '#fff',}}>
            <View style ={{flexDirection:'row',height:dheight/15,backgroundColor:'#fff',paddingLeft:18}}>
              <CheckBox style={{padding:5,marginBottom:2}}
                        onClick={ () => this.setState({ showUserImg:!this.state.showUserImg,textValue:!this.state.textValue,anonymFlage:"Y"})}
                        checkBoxColor="#2b73e1"/>
              <Text style={{marginTop:6,fontSize:16,marginLeft:5,}}>
                {this.state.askAnonText}
              </Text>
            </View>
            <View style={styles.button}>
              <TouchableOpacity activeOpacity={.5} onPress={(e) => this.formValidation()}>
                <Text style={styles.buttonText}>{Post}</Text>
              </TouchableOpacity>
            </View>
          </View>
      </KeyboardAwareScrollView>
      
      .
      .
      .
      .
      

      在导航到其他屏幕之前,只需从

      调用dismissKeyboard()方法来dismissKeyboard
      import dismissKeyboard from 'dismissKeyboard';
      

      (这个包)

      【讨论】:

      • @badugabiz 它适用于 android 和 ios。如果它不起作用,请告诉我。
      • 嗨 @badugabiz 它在 android 和 ios 中对你有用吗?
      猜你喜欢
      • 1970-01-01
      • 2017-09-11
      • 1970-01-01
      • 1970-01-01
      • 2015-08-13
      • 1970-01-01
      • 1970-01-01
      • 2013-05-14
      • 1970-01-01
      相关资源
      最近更新 更多