//这是生成QR码的示例代码//
import React, {Component} from 'react';
// import在我们的代码中做出反应。
import {
StyleSheet,
View,
TextInput,
TouchableOpacity,
Text,
} from 'react-native';
//导入所有基本组件
import QRCode from 'react-native-qrcode-svg';
class App extends Component {
constructor() {
super();
this.state = {
inputValue: '',
// TextInput的默认值
valueForQRCode: '',
};
}
getTextInputValue = () => {
//从输入中获取值的函数 //并将值设置为QRCode
this.setState({valueForQRCode: this.state.inputValue});
};
render() {
return (
<View style={styles.MainContainer}>
<QRCode
// QR码值
value={this.state.valueForQRCode ? this.state.valueForQRCode : 'NA'}
//size of QR Code
size={250}
// QR码的颜色(可选)
color="black"
// QR码的背景颜色(可选)
backgroundColor="white"
// QR码中心的徽标(可选)
/>
<TextInput
//输入以获取要在QRCode上设置的值
style={styles.TextInputStyle}
onChangeText={(text) => this.setState({inputValue: text})}
underlineColorAndroid="transparent"
placeholder="Student ID"
/>
//单击后,它将下载生成的二维码
<TouchableOpacity
onPress={this.getTextInputValue}
activeOpacity={0.7}
style={styles.button}>
<Text style={styles.TextStyle}> Generate QR Code </Text>
</TouchableOpacity>
</View>
);
}
}
export default App;
//样式
const styles = StyleSheet.create({
MainContainer: {
flex: 1,
margin: 10,
alignItems: 'center',
paddingTop: 40,
},
TextInputStyle: {
width: '100%',
height: 40,
marginTop: 20,
borderWidth: 1,
textAlign: 'center',
},
button: {
width: '100%',
paddingTop: 8,
marginTop: 10,
paddingBottom: 8,
backgroundColor: '#F44336',
marginBottom: 20,
},
TextStyle: {
color: '#fff',
textAlign: 'center',
fontSize: 18,
},
});