简体   繁体   English

如何在文本输入上自动对焦?

[英]How do I autofocus on textinput?

I made app with textinput.我用文本输入制作了应用程序。 I have mobil device with included barcode scanner.我有带有条形码扫描仪的移动设备。 I'm trying to make app with reading barcodes and writing on the server by webservice.我正在尝试通过 webservice 读取条形码并在服务器上写入来制作应用程序。 The main problem is, that i want to autofocus on textinput, cause it's boring doing Click, Scan, Click on text box and scan again,...主要问题是,我想自动聚焦于文本输入,因为单击、扫描、单击文本框并再次扫描很无聊,...

I tried everything, i googled also here on stackoverflow and i didn't found any help with my problem.我尝试了所有方法,我也在 stackoverflow 上用谷歌搜索过,但没有找到任何关于我的问题的帮助。 I tried also som libraries doing this, but also, not helpful.我也尝试过一些图书馆这样做,但也没有帮助。

import React from 'react';
import { Keyboard, View, Text, TextInput, TouchableWithoutFeedback, StyleSheet, Alert } from 'react-native';
import AsyncStorage from '@react-native-community/async-storage';

export default class HomeScreen extends React.Component{

  constructor(props){
    super(props);
    this.state = {text: '', txt:'Naskenujte regál!', txtinp:'Čiarový kód regálu', regal:'-1'};
  }
  render(){
      return(
        <TouchableWithoutFeedback onPress={Keyboard.dismiss} accessible={false}>
          <View style={{textAlign: 'center', backgroundColor: '#F5FCFF'}}>
            <Text style={styles.text}>{this.state.txt}</Text>
            <TextInput id='txt' style={styles.input}
              placeholder={this.state.txtinp}
              keyboardType="numeric"
              autoFocus={true}
              onChangeText={(text) => this._changed(text)}
              onSelectionChange={(event) => console.log(event.nativeEvent.selection)}
              value={this.state.text}
            />
          </View>
          </TouchableWithoutFeedback>
    );
  }  
  _changed = async(text) => {
    if(this.state.regal == '-1'){
      //GET REGL FROM WEBSERVICE
      //
      //
      //
      pozicia =6;
      this.setState({txt: 'Regál číslo ' + pozicia + '\nNaskenujte produkt', txtinp:'Čiarový kód produktu', regal:pozicia})

    } 
    else {
      //NACITANIE PRODUCTU Z WEBSERVICE
      //
      //
      //
      // 
      product = text
      productID = 10;

      Alert.alert(
        'Zapísanie regálu',
        'Skladové miesto ' + this.state.regal + ' je prázdne.\n\nChcete naň zapísať produkt:\n' + product + '?',
        [
          {text: 'Nie', onPress: () => this._cancel()},
          {text: 'Áno', onPress: () => this._writeToDB(this.state.regal, productID)},
        ],
        {cancelable: false},
      );
    }
  }
  _cancel = async() => {
    this.setState({text: '', txt:'Naskenujte regál!', txtinp:'Čiarový kód regálu', regal:'-1'})
  }

  _writeToDB = async(regal, productID) => {
    this._cancel();
    //WRITE TO DATABASE BY WEBSERVICE
    //
    //
    //
    //
    //
  }
}

const styles = StyleSheet.create({
  text: {
    marginTop:50,
    textAlign: 'center',
    fontSize:40,
    fontWeight:'bold',
    color:'#000000',
    marginTop:10,
    marginBottom:35
  },
  input: {
    marginTop: 15,
    marginLeft:35,
    marginRight:35,
    height:40,
    padding:5,
    fontSize:16,
    borderBottomWidth:1,
    borderBottomColor:'#f05555'
  },
});

After I open app I have login ... it gets me here... I scan and then i will make writing in to DB.在我打开应用程序后,我登录了......它让我在这里......我扫描然后我将写入数据库。 I just need, that If I scann something, it will automaticly autofocus me on the same textinput in order to scan again.我只需要,如果我扫描了一些东西,它会自动将我自动聚焦在同一个文本输入上,以便再次扫描。 The best way should be with disabling keyboard, but I don't know also how to do it.最好的方法应该是禁用键盘,但我也不知道该怎么做。 Sorry for my low experiences, but I'm new in react and mobile apps.抱歉我的经验不足,但我是 React 和移动应用程序的新手。

create a Refs to your TextInput创建一个参考文献您的TextInput

 constructor(props){
   super(props);
   this.state = {text: '', txt:'Naskenujte regál!', txtinp:'Čiarový kód regálu', 
   regal:'-1'};
   this.refTextInput = React.createRef();
 }

 <TextInput id='txt' style={styles.input}
    ...
    ref={ref => this.refTextInput = ref}
 />

then after scan you can focus to the input by call然后扫描后,您可以通过调用关注输入

this.refText.focus()

You can also use blurOnSubmit={false} in this case.在这种情况下,您还可以使用blurOnSubmit={false} This keeps the textbox focused even after the text is submitted.即使在提交文本后,这也可以保持文本框的焦点。

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

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