React Native中的简单警报-印刷功能 [英] Simple Alert in React Native - function on press

查看:80
本文介绍了React Native中的简单警报-印刷功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在React Native中,我们可以选择使用Alert来在弹出窗口中通知用户。这些简单警报可以由以下内容组成:

In React Native, we have the option to use an Alert to notify the user in a popup. These 'simple' Alerts can be composed with:

Alert.alert('Hello world!')

哪个会生成带有消息,无标题和确定按钮的警报。

Which produces an Alert with a message, no title, and an "OK" button.

您还可以制作2个或3个按钮警报,其组成如下(以2个按钮为例):

You can also make 2 or 3 button Alerts, which are composed as follows (2 button example):

Alert.alert(
  'Alert Title',
  'My Alert Msg',
  [
    {
      text: 'Ask me later', 
      onPress: () => console.log('Ask me later pressed')
    },
    {
      text: 'Cancel',
      onPress: () => console.log('Cancel Pressed'),
      style: 'cancel',
    },
    {
      text: 'OK', 
      onPress: () => console.log('OK Pressed')
    },
  ],
  {cancelable: false},
);

请注意,这里您可以选择 $ 按钮的功能

Notice here you have the options for an onPress function for the buttons

我想知道的是,是否可以在第一种情况下应用 onPress 按下确定,但中没有示例(或任何详细信息!)官方文档

What I am wondering is if I can apply an onPress for the first case, when 'OK' is pressed, but there is no example (or any details really!) in the official docs

也许(尚未)可能。有人可以确认还是拒绝?

Perhaps it is not (yet) possible. Can anyone confirm or deny?

推荐答案

您可以使用以下命令创建 Alert 只有一个按钮。然后,您可以确定在按 OK 时会发生什么。

You can create an Alert with only one button. You can then decide what happens when you press OK.

https:// facebook。 github.io/react-native/docs/alert


iOS



在iOS上,您可以指定任意数量的按钮。每个按钮可以选择
指定一种样式,该样式可以是默认,取消或
破坏性之一。

iOS

On iOS you can specify any number of buttons. Each button can optionally specify a style, which is one of 'default', 'cancel' or 'destructive'.

在Android上最多可以指定三个按钮。 Android具有
a中立,否定和肯定按钮的概念:

On Android at most three buttons can be specified. Android has a concept of a neutral, negative and a positive button:


  • 如果您指定一个按钮,它将是正(例如确定)

  • 两个按钮表示负,正(例如取消,确定)

  • 三个按钮表示中立,负,正(例如以后,
    取消,确定)

因此,如果您只想要一个按钮,则可以执行以下操作。

So if you only want one button then you can do something like this.

Alert.alert(
  'Alert Title',
  'My Alert Msg', // <- this part is optional, you can pass an empty string
  [
    {text: 'OK', onPress: () => console.log('OK Pressed')},
  ],
  {cancelable: false},
);

如果您使用 Alert.alert('Hello world!')没有传递任何选项,那么当您按下 OK 时就无法定义会发生什么,唯一的方法是执行类似我的操作显示以上。如果您希望它在屏幕上看起来一样,则只需为该消息传递一个空字符串。标题和消息都可以为空字符串,尽管您可能不希望同时使用两者。

If you use Alert.alert('Hello world!') without passing any options to it then there is no way to define what happens when you press OK the only way to do that is to do something like I have show above. If you want it to look the same on the screen, then just pass an empty string for the message. Both the title and the message can be empty strings, though you probably don't want both to be that at the same time.

这篇关于React Native中的简单警报-印刷功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆