[关闭]
@yzzer 2020-05-05T03:38:47.000000Z 字数 2189 阅读 659

React Native 弹出框 Alert

弹出框 `` 是浮于当前界面之上的,用于阻止用户的下一步操作,直到用户点击了弹出框上的任意按钮为止。

弹出框 `` 一般用于弹出 提示、弹出警告、弹出确认 等需要用户注意和确认的动作。

弹出提示

弹出提示框一般只有一个 确认 按钮,用户点击 确认 就是 我知道了的 意思。

React Native Alert

弹出警告

弹出警告框一般有两个按钮 确认 和 取消, 取消 按钮在右边,方便用户点击。

React Native Alert

弹出确认

弹出确认框一般有两个按钮 确认 和 取消, 确认 按钮在右边,方便用户点击。

React Native Alert

引入组件

  1. import { Alert } from 'react-native'

使用语法

  1. Alert.alert(title, message?, buttons?, options?, type?)

使用范例

  1. // 同时兼容 iOS 和 Android
  2. Alert.alert(
  3. '弹出框标题',
  4. '弹出框描述',
  5. [
  6. {text: '自定义按钮', onPress: () => console.log('点击了自定义按钮')},
  7. {
  8. text: '取消',
  9. onPress: () => console.log('点击了取消按钮'),
  10. style: 'cancel',
  11. },
  12. {text: '确认', onPress: () => console.log('点击了确认按钮')},
  13. ],
  14. {cancelable: false},
  15. );

范例 1 : 弹出提示

下面的代码,当我们点击 发送 按钮时会弹出一个提示 操作成功。

Step 1: App.js

  1. import React from 'react'
  2. import { Alert, Text, TouchableOpacity, StyleSheet } from 'react-native'
  3. const App = () => {
  4. const showAlert = () =>{
  5. Alert.alert('发送数据成功')
  6. }
  7. return (
  8. <TouchableOpacity onPress = {showAlert} style = {styles.button}>
  9. <Text>发送</Text>
  10. </TouchableOpacity>
  11. )
  12. }
  13. export default App
  14. const styles = StyleSheet.create ({
  15. button: {
  16. backgroundColor: '#4ba37b',
  17. width: 100,
  18. borderRadius: 50,
  19. alignItems: 'center',
  20. marginTop: 100
  21. }
  22. })

演示效果如下

范例 2: 弹出警告

下面的代码,当我们点击 删除 按钮时会弹出一个警告 是否删除。

如果用户点击了 取消 则什么事情都不做,如果点击了 确认 则会删除数据然后弹出提示

Step 1: App.js

  1. import React from 'react'
  2. import { Alert, Text, TouchableOpacity, StyleSheet } from 'react-native'
  3. const App = () => {
  4. const showTip = () => {
  5. Alert.alert('删除数据成功')
  6. }
  7. const showAlert = () =>{
  8. Alert.alert(
  9. '警告',
  10. '确认删除?',
  11. [
  12. {text: '确认', onPress: () => showTip()},
  13. {text: '取消', style: 'cancel'},
  14. ],
  15. {cancelable: false}
  16. )
  17. }
  18. return (
  19. <TouchableOpacity onPress = {showAlert} style = {styles.button}>
  20. <Text>删除</Text>
  21. </TouchableOpacity>
  22. )
  23. }
  24. export default App
  25. const styles = StyleSheet.create ({
  26. button: {
  27. backgroundColor: '#4ba37b',
  28. width: 100,
  29. borderRadius: 50,
  30. alignItems: 'center',
  31. marginTop: 100
  32. }
  33. })

演示效果如下

范例 3: 弹出确认

下面的代码,当我们点击 修改 按钮时会弹出一个确认框 是否确认。

如果用户点击了 取消 则什么事情都不做,如果点击了 确认 则会修改数据然后弹出提示

Step 1: App.js

  1. import React from 'react'
  2. import { Alert, Text, TouchableOpacity, StyleSheet } from 'react-native'
  3. const App = () => {
  4. const showTip = () => {
  5. Alert.alert('修改数据成功')
  6. }
  7. const showAlert = () =>{
  8. Alert.alert(
  9. '确认',
  10. '是否确认修改?',
  11. [
  12. {text: '取消', style: 'cancel'},
  13. {text: '确认', onPress: () => showTip()},
  14. ],
  15. {cancelable: false}
  16. )
  17. }
  18. return (
  19. <TouchableOpacity onPress = {showAlert} style = {styles.button}>
  20. <Text>修改</Text>
  21. </TouchableOpacity>
  22. )
  23. }
  24. export default App
  25. const styles = StyleSheet.create ({
  26. button: {
  27. backgroundColor: '#4ba37b',
  28. width: 100,
  29. borderRadius: 50,
  30. alignItems: 'center',
  31. marginTop: 100
  32. }
  33. })

演示效果如下

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注