简体   繁体   中英

Keyboard dosen't pop up when I click TextInput field on React Native

I am not able to get keyboard popped in IOS simulator. I have the video link if you want to have closer look. In android it was all good. Here's link for video: https://youtu.be/JKfPlXFvP6Q

Below is code for Startgamescreen.js

import React, { useState } from 'react';



import {
  View,
  Text,
  StyleSheet,
  Button,
  TouchableWithoutFeedback,
  Keyboard
} from 'react-native';


import Card from '../components/Card';
import Input from '../components/Input';
import Colors from '../constants/colors';

const StartGameScreen = props => {

const [enteredValue, setEnteredValue] = useState(''); //function which uses array to get input number from user

 const [confirmed, setConfirmed] = useState(false); // calling use state to verify user choice

const [selectedNumber, setSelectedNumber]=useState();

const numberInputHandler = inputText => {              // function to get number inputted by user 
    setEnteredValue(inputText.replace(/[^0-9]/g, '')); // excluding any character that is not from textinput
  };

const resetInputHandler = props => {
  setEnteredValue('');    // set this to empty string to reset user input in keyboard
setConfirmed(false);
};

const confirmInputHandler = () =>{
  const chosenNumber = parseInt(enteredValue);

  if (chosenNumber === NaN || chosenNumber<=0 || chosenNumber>99)    //default value of JS if its not a number
  {
    return;
  }

  setConfirmed(true);

  setSelectedNumber(chosenNumber); // saving selected number and converting it to string

  setEnteredValue('');   // resetting user input
};

 let confirmedOutput;
 if (confirmed){
   confirmedOutput= <Text> chosen number : {selectedNumber} </Text>
 }

  return (
    <TouchableWithoutFeedback
      onPress={() => {
        Keyboard.dismiss();

      }}
    >

      <View style={styles.screen}>
        <Text style={styles.title}>Start a New Game!</Text>
        <Card style={styles.inputContainer}>
          <Text>Select a Number</Text>
          <Input
            style={styles.input}
            blurOnSubmit
            autoCapitalize="none"
            autoCorrect={false}
            keyboardType="number-pad"
            maxLength={2}
            onChangeText={numberInputHandler}    // to handle user input
            value={enteredValue}
          />
          <View style={styles.buttonContainer}>
            <View style={styles.button}>
              <Button 
              title="Confirm" 
              onPress={confirmInputHandler} 
              color={Colors.accent} />
            </View>
            <View style={styles.button}>
              <Button
                title="Reset"
                onPress={resetInputHandler}
                color={Colors.primary}
              />
            </View>
          </View>
        </Card>
        {confirmedOutput}
      </View>
    </TouchableWithoutFeedback>
  );
};


const styles = StyleSheet.create({
  screen: {
    flex: 1,
    padding: 10,
    alignItems: 'center'
  },

  title: {
    fontSize: 20,
    marginVertical: 10
  },

  inputContainer: {
    width: 300,
    maxWidth: '80%',
    alignItems: 'center'
  },

  buttonContainer: {
    flexDirection: 'row',
    width: '100%',
    justifyContent: 'space-between',
    paddingHorizontal: 15
  },

  button: {
    width: 100
  },

  input: {
    width: 50,
    textAlign: 'center'
  }
});


export default StartGameScreen;

Also here's code for Input.js

import React from 'react';

import { TextInput, StyleSheet } from 'react-native';


const Input = props => {
  return <TextInput {...props} style={{ ...styles.input, ...props.style }} />;
};

const styles = StyleSheet.create({

  input: {
    height: 30,
    borderBottomColor: 'grey',
    borderBottomWidth: 1,
    marginVertical: 10
  }

});

export default Input;

I know it's a big chunk of code but I have only one problem. Any help would be appreciated.

在模拟器菜单中尝试:硬件 > 键盘 > 切换软件键盘

I am not sure in which version the menu options changed, but with Version 12.xx, the correct option is I/O > Keyboard > Toggle Software Keyboard.

Optionally you can press ⌘+K .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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