简体   繁体   English

在 Android 上,KeyboardAvoidingView 无法正确处理 TextInput 密码字段

[英]KeyboardAvoidingView not working with TextInput password fields Properly on Android

I am using react-native-paper for Text Input and using KeyboardAvoidingView to remove keyboard issues and put Input fields above the KeyBoard , And this is working for other TextInput fields as expected not with the password fields, But When I remove secureTextEntry={true} this works fine on android, but this is not the solution as this line is mandatory in Password fields.我正在使用react-native-paper进行Text Input并使用KeyboardAvoidingView来消除keyboard issues并将Input字段放在KeyBoard上方,这对其他TextInput字段有效,而不适用于密码字段,但是当我删除secureTextEntry={true}这适用于Android精品,但由于此行是在密码字段强制性这样是解决不了问题。 I also tried many libraries likereact-native-keyboard-aware-scroll-view我也尝试过很多库,比如react-native-keyboard-aware-scroll-view

    /* @flow */

import * as React from 'react';
import {
  StyleSheet,
  ScrollView,
  KeyboardAvoidingView,
} from 'react-native';
import { TextInput, Button} from 'react-native-paper';


export default class App extends React.Component {
  static title = 'TextInput';

  state = {
    name: '',
    lastName:'',
    phone:'',
    email:'',
    states:'',
    password:'',
    repeatPassword:'',
  };

  render() {

    return (
      <KeyboardAvoidingView
        style={styles.wrapper}
        behavior="padding"
        keyboardVerticalOffset={80}
      >
        <ScrollView
          style={[styles.container, { backgroundColor: '#bdda' }]}
          keyboardShouldPersistTaps={'always'}
          removeClippedSubviews={false}
        >
          <TextInput
            style={styles.inputContainerStyle}
            label="First Name"
            placeholder="Type First Name"
            value={this.state.name}
            onChangeText={name => this.setState({ name })}
            returnKeyType={"next"}
            onSubmitEditing={() => { this.lastName.focus() }}
            blurOnSubmit={false}
          />

          <TextInput
            style={styles.inputContainerStyle}
            label="Last Name"
            placeholder="Type Last Name"
            value={this.state.lastName}
            onChangeText={lastName => this.setState({ lastName })}
            ref={(input) => { this.lastName = input; }}
            onSubmitEditing={() => { this.phone.focus() }}
            returnKeyType={"next"}
            blurOnSubmit={false}
          />

          <TextInput
            style={styles.inputContainerStyle}
            label="Phone"
            placeholder="Type Phone"
            value={this.state.phone}
            onChangeText={phone => this.setState({ phone })}
            ref={(input) => { this.phone = input; }}
            onSubmitEditing={() => { this.email.focus() }}
            returnKeyType={"next"}
            blurOnSubmit={false}
          />

          <TextInput
            style={styles.inputContainerStyle}
            label="Email"
            placeholder="Type Email"
            value={this.state.email}
            onChangeText={email => this.setState({ email })}
            ref={(input) => { this.email = input; }}
            onSubmitEditing={() => { this.states.focus() }}
            returnKeyType={"next"}
            blurOnSubmit={false}

          />

          <TextInput
            style={styles.inputContainerStyle}
            label="State"
            placeholder="Type State"
            value={this.state.states}
            onChangeText={states => this.setState({ states })}
            ref={(input) => { this.states = input; }}
            onSubmitEditing={() => { this.password.focus() }}
            returnKeyType={"next"}
            blurOnSubmit={false}
          />

          <TextInput
            style={styles.inputContainerStyle}
            label="Password"
            placeholder="Type Password"
            secureTextEntry={true}
            value={this.state.password}
            onChangeText={password => this.setState({ password })}
            ref={(input) => { this.password = input; }}
            onSubmitEditing={() => { this.repeatPassword.focus() }}
            returnKeyType={"next"}
            blurOnSubmit={false}
          />

          <TextInput
            style={styles.inputContainerStyle}
            label="Repeat Password"
            placeholder="Type Repeat Password"
            secureTextEntry={true}
            value={this.state.repeatPassword}
            onChangeText={repeatPassword => this.setState({ repeatPassword })}
            ref={(input) => { this.repeatPassword = input; }}
            returnKeyType={"done"}
            blurOnSubmit={true}
          />

          <Button onPress={() => alert('I am pressed :P-')} style={{marginTop:20}}>
              Submit
            </Button>
        </ScrollView>
      </KeyboardAvoidingView>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    padding: 25,
    marginTop:40,
  },
  wrapper: {
    flex: 1,
  },
  inputContainerStyle: {
    margin: 8,
  },
});

To check this issue I also created Demo Project , This issue is only in Android, In belowed Screen Shot Password text fields is active, but password fields is hidden,为了检查这个问题,我还创建了Demo Project ,这个问题仅在 Android 中,在下面的屏幕截图Password text fields是活动的,但密码字段是隐藏的, 截屏 . .

Had the same problem.有同样的问题。 I managed it by setting secureTextEntry prop to true only when the password input is focused.我通过仅在密码输入为焦点时将 secureTextEntry prop 设置为 true 来管理它。

 const [secureTextEntry, setSecureTextEntry] = useState(false)

 ...

<TextInput
     style={styles.inputContainerStyle}
     label="Repeat Password"
     placeholder="Type Repeat Password"
     secureTextEntry={secureTextEntry}
     value={this.state.repeatPassword}
     onFocus={() => setSecureTextEntry(true)}
     onChangeText={repeatPassword => this.setState({ repeatPassword })}
     ref={(input) => { this.repeatPassword = input; }}
     returnKeyType={"done"}
     blurOnSubmit={true}
 />

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

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