我正在开发react-native应用程序,并尝试制作自定义警报方法(例如react-native官方网站中给出的Alert。alert())。 React-native警报没有 Font Size和背景色的样式属性,当我在android标签上使用时,警报很小,但是在android扫描仪和移动设备上使用时效果很好。
enter code here
import React, { Component } from 'react';
import { StyleSheet, Platform, View, Text,TouchableOpacity } from 'react-native';
// import PropTypes from 'prop-types';
// import Dialog from "react-native-dialog";
import Alertfunction from './src/CustomAlert'
export default class App extends Component{
render() {
return (
<Alertfunction Title={"Alert"} FontSize = {30} FontColor= '#FF9800' Visible={true}/>
);
}
}
const styles = StyleSheet.create({
MainContainer :{
flex:1,
paddingTop: (Platform.OS) === 'ios' ? 20 : 0,
alignItems: 'center',
justifyContent: 'center',
}
});
customAlert.js
import React, { Component } from 'react';
import { StyleSheet, Platform, View, Text,TouchableOpacity } from 'react-native';
import PropTypes from 'prop-types';
import Dialog from "react-native-dialog";
class Alertfunction extends Component {
state = {
dialogVisible: this.props.Visible
};
showDialog = () => {
this.setState({ dialogVisible: this.props.Visible });
};
handleCancel = () => {
this.setState({ dialogVisible: false });
// this.props.Visible=false;
};
handleDelete = () => {
this.setState({ dialogVisible: false });
//this.props.Visible=false;
};
render() {
return (
<View>
<TouchableOpacity onPress={this.showDialog}>
<Text >{this.props.Title}</Text>
</TouchableOpacity>
<Dialog.Container visible={this.state.dialogVisible}>
<Dialog.Title style={{fontSize : this.props.FontSize, color: this.props.FontColor}}>{this.props.Title}</Dialog.Title>
<Dialog.Description style={{fontSize : this.props.FontSize, color: this.props.FontColor}}>
Do you want to delete this account? You cannot undo this action.
</Dialog.Description>
<Dialog.Button style={{fontSize : this.props.FontSize, color: this.props.FontColor}} label="Cancel" onPress={this.handleCancel} />
<Dialog.Button style={{fontSize : this.props.FontSize, color: this.props.FontColor}} label="ok" onPress={this.handleDelete} />
</Dialog.Container>
</View>
);
}
}
export default Alertfunction;
Alertfunction.propTypes =
{
Title: PropTypes.string,
FontSize: PropTypes.number,
FontColor: PropTypes.string,
Visible: PropTypes.bool,
}
Alertfunction.defaultProps =
{
Title: "Default Name",
FontColor: "#00E676",
FontSize: 15,
Visible:false
}
const styles = StyleSheet.create({
MainContainer :{
flex:1,
paddingTop: (Platform.OS) === 'ios' ? 20 : 0,
alignItems: 'center',
justifyContent: 'center',
}
});
答案 0 :(得分:0)
您需要在父组件中声明方法,并像下面这样作为props传递: 并使用这些方法更新dialogVisible的值。
import React, { Component } from 'react';
import { StyleSheet, Platform, View, Text,TouchableOpacity } from 'react-native';
import Alertfunction from './src/CustomAlert'
export default class App extends Component{
this.state = {
dialogVisible: true;
}
showDialog = () => {
this.setState({ dialogVisible: this.props.Visible });
};
handleCancel = () => {
this.setState({ dialogVisible: false });
};
handleDelete = () => {
this.setState({ dialogVisible: false });
};
render() {
const { dialogVisible } = this.state;
return (
<Alertfunction
Title={"Alert"}
FontSize = {30}
FontColor= '#FF9800'
Visible={dialogVisible}
showDialog={this.showDialog}
handleCancel={this.handleCancel}
handleDelete={this.handleDelete}
/>
);
}
}
const styles = StyleSheet.create({
MainContainer :{
flex:1,
paddingTop: (Platform.OS) === 'ios' ? 20 : 0,
alignItems: 'center',
justifyContent: 'center',
}
});
并在子组件中调用这些方法以更新 dialogVisible
的值您的子组件应如下所示:
customAlert.js
import React, { Component } from 'react';
import { StyleSheet, Platform, View, Text,TouchableOpacity } from 'react-native';
import PropTypes from 'prop-types';
import Dialog from "react-native-dialog";
class Alertfunction extends Component {
render() {
const { Visible } = this.props;
return (
<View>
<TouchableOpacity onPress={this.props.showDialog}>
<Text >{this.props.Title}</Text>
</TouchableOpacity>
<Dialog.Container visible={Visible}>
<Dialog.Title style={{fontSize : this.props.FontSize, color: this.props.FontColor}}>{this.props.Title}</Dialog.Title>
<Dialog.Description style={{fontSize : this.props.FontSize, color: this.props.FontColor}}>
Do you want to delete this account? You cannot undo this action.
</Dialog.Description>
<Dialog.Button style={{fontSize : this.props.FontSize, color: this.props.FontColor}} label="Cancel" onPress={this.props.handleCancel} />
<Dialog.Button style={{fontSize : this.props.FontSize, color: this.props.FontColor}} label="ok" onPress={this.props.handleDelete} />
</Dialog.Container>
</View>
);
}
}
export default Alertfunction;
Alertfunction.propTypes =
{
Title: PropTypes.string,
FontSize: PropTypes.number,
FontColor: PropTypes.string,
Visible: PropTypes.bool,
}
Alertfunction.defaultProps =
{
Title: "Default Name",
FontColor: "#00E676",
FontSize: 15,
Visible:false
}
const styles = StyleSheet.create({
MainContainer :{
flex:1,
paddingTop: (Platform.OS) === 'ios' ? 20 : 0,
alignItems: 'center',
justifyContent: 'center',
}
});
答案 1 :(得分:0)
我通过制作自定义控件解决了此问题,这里是link