react-native两个文本框输入相同文本后警告并自动清空
时间: 2023-05-23 08:04:18 浏览: 79
A:可以在文本框的onChangeText事件中加入判断,如果两个文本框的值相等,则弹出警告提示并清空文本框。具体代码如下:
```
import React, { useState } from 'react';
import { View, Text, TextInput, Alert } from 'react-native';
const Example = () => {
const [text1, setText1] = useState('');
const [text2, setText2] = useState('');
const handleTextChange1 = (text) => {
setText1(text);
if (text === text2) {
Alert.alert('警告', '两个文本框的值不能相同', [{ text: '确定' }]);
setText1('');
setText2('');
}
};
const handleTextChange2 = (text) => {
setText2(text);
if (text === text1) {
Alert.alert('警告', '两个文本框的值不能相同', [{ text: '确定' }]);
setText1('');
setText2('');
}
};
return (
<View>
<TextInput value={text1} onChangeText={handleTextChange1} />
<TextInput value={text2} onChangeText={handleTextChange2} />
</View>
);
};
export default Example;
```
阅读全文