【问题标题】:Onpress() of native-base Button is not working on Android本机基础按钮的 Onpress() 在 Android 上不起作用
【发布时间】:2021-03-15 08:17:16
【问题描述】:

我正在使用原生按钮并尝试通过按下按钮来下载图像。它在 IOS 中运行良好,但在 Android 中无法运行。这是我的代码

        <View
            style={{
              marginVertical: hp("2%"),
              marginHorizontal: wp("15%"),
              textAlign: "center",
              width: "100%",
            }}
          >
            <Button
              onPress={() => downloadFile()}
              style={{
                backgroundColor: "#fff",
                width: "60%",
                borderRadius: 0,
              }}
              block
            >
              <Text
                style={{
                  color: "black",
                  fontSize: RFPercentage(2.1),
                  fontFamily: "BalsamiqSansBold",
                }}
              >
                ADD TO PHOTOS
              </Text>
            </Button>
          </View>

在 IOS 中按下按钮时会调用 downloadFile() 函数,但在 Android 中它不起作用。

我也尝试过这样的控制台登录新闻

          <Button
              onPress={() => console.log("Button Clicked")}
              style={{
                backgroundColor: "#fff",
                width: "60%",
                borderRadius: 0,
              }}
              block
            >

它在 IOS 中有效,但在 Android 中无效。

此外,如果我在 Android 中像这样直接调用 onPress 上的函数,它会自动记录 Button Clicked 而不被按下。我试图找出所有可能的方法来解决它,但我没有得到这个问题的确切原因。

         <Button
              onPress={console.log("Button Clicked")}
              style={{
                backgroundColor: "#fff",
                width: "60%",
                borderRadius: 0,
              }}
              block
            >

【问题讨论】:

    标签: react-native button react-native-android react-native-ios onpress


    【解决方案1】:

    也许您在 android 中遇到了有关 TouchableOpacity 的问题。

    您可以从“react-native-gesture-handler”导入 ToucahableOpacity,希望这会有所帮助。

    你可以这样导入

    import {TouchableOpacity} from 'react-native-gesture-handler'
    

    【讨论】:

    • 感谢您的建议 Awais Ibrar,实际上问题在于按钮在 Android 中的位置,因为它没有被按下。
    • 没问题。很高兴听到您的问题已解决。
    【解决方案2】:
    import {TouchableOpacity } from 'react-native'
         
    <TouchableOpacity
                    style={styles.button}
                    onPress={handleSaveLimitValue}
                  >
                    <Text
                      color={colors.white}
                      weight={TextWeight.DEMI_BOLD}
                      size={16}
                      style={styles.buttonText}
                    >
                      Save
                    </Text>
                  </TouchableOpacity>
    

    使用原生模块的 TouchableOpacity 对我有用,而不是使用 import {BaseButton} from react-native-gesture-handler

    希望我的回答对你有所帮助。

    【讨论】:

      猜你喜欢
      • 2019-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-23
      相关资源
      最近更新 更多