react-native弹窗Alert

刺骨的言语ヽ痛彻心扉 2022-05-22 10:06 357阅读 0赞

学习交流:https://gitee.com/potato512/Learn_ReactNative

react-native学习交流QQ群:806870562

效果图

70

代码示例

  1. import React from 'react';
  2. import {View, Text, Button, Alert, AlertIOS} from "react-native";
  3. const title = "温馨提示";
  4. const message = '要下雨了,记得带伞'
  5. export default class AlertPage extends React.Component {
  6. constructor(props) {
  7. super(props);
  8. _this = this;
  9. this.state = {
  10. inputText:'',
  11. };
  12. }
  13. render(){
  14. return(
  15. <View style={
  16. {marginTop:60, alignItems:"center"}}>
  17. <Text>Alert的使用</Text>
  18. <Button title="标题+确定" onPress={() => {
  19. Alert.alert(title)
  20. }}/>
  21. <Button title="标题+内容+确定" onPress={() => {
  22. Alert.alert(title,message)
  23. }}/>
  24. <Button title="标题+内容+取消+确定" onPress={() => {
  25. Alert.alert(
  26. title,
  27. message,
  28. [
  29. {text: '不带伞', onPress: () => console.log('Cancel Pressed'), style: 'cancel'},
  30. {text: '知道了', onPress: () => console.log('OK Pressed')},
  31. ],
  32. )
  33. }}/>
  34. <Button title="标题+内容+多按钮" onPress={() => {
  35. Alert.alert(
  36. title,
  37. message,
  38. [
  39. {text: '不带伞', onPress: () => console.log('Cancel Pressed'), style: 'cancel'},
  40. {text: '知道了', onPress: () => console.log('OK Pressed')},
  41. {text: '有人送伞', onPress: () => console.log('送伞')},
  42. ],
  43. {
  44. cancelable: true,
  45. onDismiss: () => {
  46. ToastAndroid.show('点击了外面', ToastAndroid.SHORT)
  47. }
  48. }
  49. )
  50. }}/>
  51. <Button title="iOS" onPress={() => {
  52. AlertIOS.alert(
  53. title,
  54. message,
  55. [
  56. {text: '取消',onPress: function() {console.log('取消按钮点击');}},
  57. {text: '确认',onPress: function() {console.log('确认按钮点击');}},
  58. ]
  59. )
  60. }}/>
  61. <Button title="iOS+输入框" onPress={() => {
  62. AlertIOS.prompt(
  63. title,
  64. message,
  65. [
  66. {text: '取消',onPress: function() {console.log('取消按钮点击');}},
  67. {text: '确认',onPress: (text) => {
  68. _this.setState({inputText:text})
  69. console.log('你输入了: ' + _this.state.inputText)
  70. }},
  71. ],
  72. 'secure-text',
  73. this.state.inputText,
  74. 'number-pad'
  75. )
  76. }}/>
  77. )
  78. </View>
  79. )
  80. }
  81. }

发表评论

表情:
评论列表 (有 0 条评论,357人围观)

还没有评论,来说两句吧...

相关阅读

    相关 封装alert样式

    开发过程中,有些样式也是要注重美观的,对此,我们可以对一些基层方法做一些样式处理,是效果看起来更加的符合整体样式。 function alertInfo(message)\{