【问题标题】:Keyboard Focus() not working in react native键盘焦点()在本机反应中不起作用
【发布时间】:2019-12-18 21:25:24
【问题描述】:

在这里,我正在尝试做非常基本的事情。首先,我专注于 TextInput,同时专注于我正在使用 imagepicker 拍照。在我拿走它并回来之后我的键盘变得隐藏起来。保存图像后,我使用了 focus() 方法。在 iOS 中,它得到了关注。但在android中它不起作用。我需要再次触摸 textinput 才能打开它。是 android 平台的问题还是我错了请告诉我。示例代码如下。谢谢。

renderView(){
  return(
    <View>
    <TouchableOpacity style={{marginLeft:28}} onPress={()=>{this.selectPhotoTapped()}}></TouchableOpacity>
    <TextInput
      style={{ maxHeight: Math.min(this.state.height+20,250),
                        height: Math.min(this.state.height+20,250)}}
      placeholderTextColor="#aaaaaa"
      autoGrow={true}
      autoFocus={false}
      multiline= {true}
      ref={ref => (this.ref = ref)}
      onChangeText={( postText) => this.setState({ postText})}
    />
    <View>                               
  )
}

selectPhotoTapped() {

    const options = {
      quality: 1.0,
      maxWidth: 500,
      maxHeight: 500,
      storageOptions: {
      skipBackup: true
      }       
    };

    ImagePicker.showImagePicker(options, (response) => { 
      console.log('Response = ', response);

      if (response.didCancel) {

        console.log('User cancelled photo picker');
      }
      else if (response.error) {

        console.log('ImagePicker Error: ', response.error);
      }
      else if (response.customButton) {

        console.log('User tapped custom button: ', response.customButton);
      }
      else {
        let mediaType = response.type

        this.setState({
          mediaAttached: true,
          attachedMediaType: 2,
          mediaPath:response.uri,
          uploadMediaType:mediaType,           
        });
        this.UploadSelectedImageForGT()

      }

    });   

}

UploadSelectedImageForGT(){
    this.setState({ visiblePostsomething:false, mediaUploading: true})
    this.ref.focus(); 
      const uuidv4 = require('uuid/v4');
      if(this.state.mediaPath!= ''){
        try{
          const storage = firebase.storage();
          this.setState({fireBaseMediaPath: 'Appsurvey/Image/user1/'+uuidv4()+'img.jpg'})
            const mRef = storage.ref('portal1').child(this.state.fireBaseMediaPath);
            mRef.putFile(this.state.mediaPath, { contentType: this.state.uploadMediaType })
            .on('state_changed', snapshot => {
                                  }, err => {
                                  }, uploadedFile => {
                                    console.log('uploadedFile.downloadURL: ', uploadedFile.downloadURL);                                
                                    this.setState({PostMediaURL: uploadedFile.downloadURL, uploadSuccess: true, mediaUploading: false}) 
                                });
          }catch(error){
              Alert.alert(error)
          }
      }      
  }

【问题讨论】:

    标签: javascript react-native focus textinput react-native-image-picker


    【解决方案1】:

    你可以试试React.createRef()

      constructor(props) {
        super(props);
        this.textInput = React.createRef();
      }
    ...
        <TextInput
          style={{ maxHeight: Math.min(this.state.height+20,250),
                            height: Math.min(this.state.height+20,250)}}
          placeholderTextColor="#aaaaaa"
          autoGrow={true}
          autoFocus={false}
          multiline= {true}
          ref={this.textInput}
          onChangeText={( postText) => this.setState({ postText})}
        />
    ...
    UploadSelectedImageForGT(){
    ...
    this.textInput.current.focus();
    ...
    }
    

    示例

    import * as React from 'react';
    import { Text, View, StyleSheet,TextInput,Button } from 'react-native';
    
    export default class App extends React.Component {
      constructor(props) {
        super(props);
        this.textInput = React.createRef();
        this.textInput2 = React.createRef();
        this.focusTextInput = this.focusTextInput.bind(this);
      }
    
      focusTextInput() {
    
        this.textInput.current.focus();
      }
    
      render() {
        return (
          <View style={{flex:1,justifyContent:"center",alignItems:"center"}}>
              <TextInput ref={this.textInput} style={{width:"80%", height:30,backgroundColor:"blue",color:"#ffffff"}}/>
              <TextInput ref={this.textInput2} style={{width:"80%", height:30,backgroundColor:"red",color:"#ffffff"}}/>
              <Button title="focus button" onPress={this.focusTextInput}/>
          </View>
        );
      }
    }
    

    【讨论】:

    • @sd_dewasurendra 你想成为第一个运行函数的人吗?
    • 我没明白
    • @sd_dewasurendra 这个函数运行正常吗?
    • 是的,它运行良好。唯一的问题是焦点方法在 android 平台上不起作用。功能在 android 和 iOS 平台上都可以正常工作
    • 你能试试这个吗? this.UploadSelectedImageForGT(this.textInput) ... UploadSelectedImageForGT(text){ text.current.focus();
    【解决方案2】:

    如果不自己运行您的代码,我的猜测是您需要将您的 this.ref.focus(); 移动到您的 setState 的回调中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-22
      • 1970-01-01
      • 1970-01-01
      • 2018-12-10
      • 2018-01-04
      • 2017-01-23
      • 2021-12-08
      • 1970-01-01
      相关资源
      最近更新 更多