Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native React Native中InputField问题的onChangeText_React Native - Fatal编程技术网

React native React Native中InputField问题的onChangeText

React native React Native中InputField问题的onChangeText,react-native,React Native,我正在开发一个ReactNative应用程序。我有一些输入字段,单击按钮,如果其中任何字段为空,我必须显示验证错误。我的代码是: <View> <InputField placeholder="Teilnummer" onChangeText={(nm)=> this.setState({pTeilnummer: nm})} /> <View> {

我正在开发一个ReactNative应用程序。我有一些输入字段,单击按钮,如果其中任何字段为空,我必须显示验证错误。我的代码是:

<View>
        <InputField placeholder="Teilnummer"
          onChangeText={(nm)=>  this.setState({pTeilnummer: nm})}
        />
        <View>
        {
          this.state.ErrorTeilnummer ? <Text style={styles.ValidationErrorMessage}> * Required </Text> : null
        }
        </View>

        <InputField placeholder="Teilbenennung"
          onChangeText={(nm)=>  this.setState({pTeilbenennung: nm})}
        />
        <View>
        {
          this.state.ErrorTeilbenennung ? <Text style={styles.ValidationErrorMessage}> * Required </Text> : null
        }
        </View>

        <InputField placeholder="Lieferant"
          onChangeText={(nm)=>   this.setState({pLieferant: nm})}
        />
        <View>
        {
          this.state.ErrorLieferant ? <Text style={styles.ValidationErrorMessage}> * Required </Text> : null
        }
        </View>

<Button title="Speichern" color="#ee7100" onPress={() => { this._addProject(this.state.pTeilnummer +  "_" + this.state.pTeilbenennung + "_" +  this.state.pLieferant, this.state.pdate)}}/>
</View>
我的按钮点击代码是:

_addProject = (name, date) =>{

  teilbenennung = this.state.pTeilbenennung;
  teilnummer = this.state.pTeilnummer;
  lieferant = this.state.pLieferant;
  var selectedCheck = this.state.addCheck

  if(teilbenennung == null || teilnummer == null || lieferant == null)
  {
    if(teilbenennung == null)
      this.setState({ErrorTeilbenennung: true})
    else
      this.setState({ErrorTeilbenennung: false})

    if(teilnummer == null)
      this.setState({ErrorTeilnummer: true})
    else
      this.setState({ErrorTeilnummer: false})

    if(lieferant == null)
      this.setState({ErrorLieferant: true})
    else
      this.setState({ErrorLieferant: false})
  }
  else
  { // saving to database
  }
}

当我第一次点击按钮时,我得到了所需的验证错误。但在我在第一个InputField中键入一些内容并按下按钮后,我仍然会收到该字段的验证错误(可能是“onChangeText”有问题),但如果我再次单击按钮,则该字段的验证错误将被删除(我的意思是我必须单击两次)。让我再添加一件事,当我单击InputField在其中写入内容时,组件将重新加载。如何删除此问题,使其可以通过单击正常工作?我举了一个示例来解决您的问题。这就是你解决你想要的问题的方法

单独比较状态值是一种非常不方便的方法。通过
onChangeText

示例在这里

import React,{Component}来自'React';
从“react native”导入{AppRegistry,TextInput,View,Text,Button};
导出默认类UselessTextInput扩展组件{
建造师(道具){
超级(道具);
this.state={
文本:“”,错误1:“错误1”,
text2:'',error2:“error2”};
}
checkfunc(){
if(this.state.text!=''&&this.state.text2!=''){
警惕(“成功”);
}否则{
警报(“失败”);
}
}
render(){
返回(
this.setState({text})}
值={this.state.text}
/>
{this.state.text!=''?null:({this.state.error1})}
this.setState({text2})
值={this.state.text2}
/>
{this.state.text2!=''?null:({this.state.error2})}
this.checkfunc()}/>
);
}
}
//如果使用CreateReact本机应用程序,请跳过此行
AppRegistry.registerComponent('AwesomeProject',()=>UselessTextInput);

问题是我的视图在键盘awarScrollView中,所以第一次单击会关闭键盘,这就是为什么我的按钮单击不会在第一次单击时触发。这可以通过两种方式解决:

  • 使用ScrollView而不是KeyboardawarScrollView
  • 键盘的持续点击设置为true,如下所示:


where is
local\u p\u id
projects
这些都是简单的对象,我编辑了这个问题我的理解是“InputField”有一些问题,在写了一些东西之后,你必须点击其他地方,这样它才能失去焦点,只有在这之后它才能工作(将值存储在状态中)@HongDevelop您能尝试将NULL的所有状态值更改为空吗?单击按钮的条件语句也应该更改。谢谢,您的意思是我应该使用“TextInput”而不是“InputField”。??因为到目前为止,我所理解的是,在不失去焦点的情况下,“InputField”将给出空文本InputField也可以使用,因为它有像Textinput这样的onChangeText道具。
_addProject = (name, date) =>{

  teilbenennung = this.state.pTeilbenennung;
  teilnummer = this.state.pTeilnummer;
  lieferant = this.state.pLieferant;
  var selectedCheck = this.state.addCheck

  if(teilbenennung == null || teilnummer == null || lieferant == null)
  {
    if(teilbenennung == null)
      this.setState({ErrorTeilbenennung: true})
    else
      this.setState({ErrorTeilbenennung: false})

    if(teilnummer == null)
      this.setState({ErrorTeilnummer: true})
    else
      this.setState({ErrorTeilnummer: false})

    if(lieferant == null)
      this.setState({ErrorLieferant: true})
    else
      this.setState({ErrorLieferant: false})
  }
  else
  { // saving to database
  }
}