【问题标题】:How to make keyboard open automatically in android without clicking on it如何在不点击的情况下在android中自动打开键盘
【发布时间】:2021-01-17 00:03:57
【问题描述】:

对我来说,键盘应该在不点击的情况下自动打开,我使用了 react-navigation,所以从一个屏幕到另一个屏幕时自动对焦不起作用

我试过了:

import React, {Component} from 'react';
import {
  View,
  StyleSheet,
  TextInput,
  TouchableOpacity,
  Keyboard,
} from 'react-native';

export default class App extends Component {
  constructor(props) {
    super(props);
    this.buildNameTextInput = React.createRef();
  }
  render() {
    return (
      <View style={styles.container}>
        <View
          style={{
            backgroundColor: 'white',
            alignItems: 'center',
            flexDirection: 'row',
            width: '90%',
            paddingHorizontal: 10,
          }}>
          <TouchableOpacity />
          <TextInput
            autoFocus={!!this.buildNameTextInput}
            ref={this.buildNameTextInput}
            style={{backgroundColor: 'white', width: '80%'}}
          />
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#8ee3ab',
  },
});

但是如果我使用它在进入下一个屏幕后它不起作用, 如果有人有任何解决方案请发布, 提前谢谢....

【问题讨论】:

    标签: android keyboard react-native-android autofocus


    【解决方案1】:

    使用引用聚焦文本输入 this.refname.current.foicus()

    检查下面的代码

    export default class App extends Component {
      constructor(props) {
        super(props);
        this.input = React.createRef();
      }
    
    componentDidMount() {
      this.input.current.focus()
    }
      render() {
        return (
          <View style={styles.container}>
            <View
              style={{
                backgroundColor: 'white',
                alignItems: 'center',
                flexDirection: 'row',
                width: '90%',
                paddingHorizontal: 10,
              }}>
              <TouchableOpacity />
              <TextInput
                ref={this.input}
                style={{backgroundColor: 'white', width: '80%'}}
              />
            </View>
          </View>
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-14
      • 1970-01-01
      • 2014-02-04
      • 1970-01-01
      • 1970-01-01
      • 2014-07-16
      • 2011-12-26
      相关资源
      最近更新 更多