在 React Native 应用程序中可按下不可触摸

如何解决在 React Native 应用程序中可按下不可触摸

我有一些可按元素出现在我的 iPhone 上 Expo 上,但根本不可触摸。我什至无法在元素检查器中单击它们。任何人都可以指导我解决问题吗?我想知道它是否是嵌套及其父容器影响它的行为,因为当我将 TouchableWithoutFeedback 更改为视图时,它会立即“出现”并按预期工作。为什么“TouchableWithoutFeedback”元素会取消我的 pressable 元素而不是 TouchableOpacity 元素?根据文档,“TouchableWithoutFeedback 只支持一个孩子”,但是将所有内容都放在一个视图中也不能解决 pressable 的这个问题。由于它的工作原理,似乎 pressable 不能存在于这个可触摸元素中?

代码如下:

import React from "react";
import { Dimensions,StyleSheet,Text,View,Image,TextInput,pressable,KeyboardAvoidingView,Keyboard } from "react-native";
import { TouchableOpacity,TouchableWithoutFeedback } from "react-native-gesture-handler";
const { width,height } = Dimensions.get("window");
const color = "#d00000";

const errorMessage = () => {
  console.log("errorMessage triggered");
  Alert.alert("Please enter a name for your character");
};

const Home = (props) => {
  return (
    <KeyboardAvoidingView style={styles.container} behavior='padding'>
      <TouchableWithoutFeedback onPress={Keyboard.dismiss} accessible={false}>

        <Image style={styles.logo} source={require("../assets/logo.jpg")} />
        <View style={styles.main}>
          <Text style={styles.titulo}>JW's D&D 5e</Text>
          <Text style={styles.subtitulo}>Character Generator</Text>

          <View style={styles.bodyText}>
            <Text style={styles.subtitulo}>Welcome!</Text>
            <Text style={styles.text}>On the following screens you'll choose premade basics for your new character: Race,class,background,plus a photo uploaded from your camera library.</Text>
            <Text style={styles.subtext}>(Your info is stored locally in your device until you 'log out' within the app.)</Text>
          </View>

          {/* DIVIDER */}
          <View
            style={{
              width: "80%",marginTop: 10,marginBottom: 10,borderBottomWidth: StyleSheet.hairlinewidth,borderBottomColor: "#000",alignSelf: "center",}}
          />

          {props.loggedIn === true ? (
            <pressable
              hitSlop={20}
              pressRetentionOffset={{ top: 20,right: 20,bottom: 20,left: 20 }}
              onPress={() => props.logout()}
              style={({ pressed }) => [
                {
                  backgroundColor: pressed ? "dodgerblue" : color,},styles.pressable,]}>
              {({ pressed }) => <Text style={styles.btnText}>{pressed ? "Later!" : "logout"}</Text>}
            </pressable>
          ) : (
            <View style={styles.nameInput}>
              <Text style={styles.text}>Enter your character's name: </Text>
              <View style={styles.input}>
                <TextInput placeholder='Character name' value={props.name} onChangeText={(text) => props.setName(text)} style={styles.inputText} />
              </View>

              <pressable
                hitSlop={40}
                pressRetentionOffset={{ top: 20,left: 20 }}
                onPress={() => {
                  props.login();
                  !props.name ? errorMessage() : props.navigation.navigate("Race");
                }}
                style={({ pressed }) => [
                  {
                    backgroundColor: pressed ? "dodgerblue" : color,]}>
                {({ pressed }) => <Text style={styles.btnText}>{pressed ? "Let's go!" : "Start your adventure!"}</Text>}
              </pressable>
            </View>
          )}
        </View>

      </TouchableWithoutFeedback>
    </KeyboardAvoidingView>
  );
};

export default Home;

const styles = StyleSheet.create({
  container: {
    flex: 1,flexDirection: "column",backgroundColor: "white",// alignItems: "center",justifyContent: "center",width: width,height: height,bodyText: {
    width: "95%",marginTop: 20,alignItems: "center",btnText: {
    color: "white",fontSize: 24,input: {
    height: 50,width: 200,borderWidth: 1,borderColor: "#ccc",inputText: {
    width: "100%",height: "100%",textAlign: "center",logo: {
    height: 70,resizeMode: "contain",main: {
    width: width,nameInput: {
    alignItems: "center",pressable: {
    marginTop: 20,height: 40,borderRadius: 5,shadowColor: "rgba(0,.3)",// IOS
    shadowOffset: { height: 3,width: 3 },// IOS
    shadowOpacity: 1,// IOS
    shadowRadius: 1,//IOS
    elevation: 2,// Android
    zIndex: 3,text: {
    color: "#9d0208",lineHeight: 40,titulo: {
    color: color,fontSize: 30,fontWeight: "bold",subtext: {
    textAlign: "center",lineHeight: 24,fontSize: 16,width: "70%",subtitulo: {
    marginTop: 10,color: color,});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?