【问题标题】:TouchableOpacity onpress is not working with NavigationTouchableOpacity onpress 不适用于导航
【发布时间】:2020-07-21 13:47:56
【问题描述】:

我正在使用 react native Navigation,并且在使用 touchableOpacity onPress 道具时在标题中放置了一个开关以在明暗主题之间切换。没有错误日志,当我按下开关时,touchableOpacity onpress 没有启动。我确实分享了我的 App.js 代码,如果您能看到并指出我做错的地方,我将不胜感激。

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 * @flow strict-local
 */

import React, {useState} from 'react';
import {StatusBar, View, TouchableOpacity} from 'react-native';
import {NavigationContainer} from '@react-navigation/native';
import Home from './src/screens/Home';
import Details from './src/screens/Details';
import {createStackNavigator} from '@react-navigation/stack';
import {DarkTheme, Text, Title, TouchableRipple, Switch} from 'react-native-paper';
import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';

const Stack = createStackNavigator();


const App = () => {

  const [isDartheme, setDarkTheme] = useState(false);

  const togglemethod = () => {
      console.log("Called!");
    setDarkTheme(!isDartheme);
  };


  return (
    <>

          
      <NavigationContainer>
        <StatusBar barStyle="dark-content" />
        <Stack.Navigator screenOptions={{headerTitleAlign: 'center'}}>
          <Stack.Screen
            name="Home"
            component={Home}
            options={{
              headerTitle: (props) => (
                <View style={{ flexDirection: 'row'}}>
                <View>
                  <Title style={{paddingLeft: 130}}>
                    <Text>Home</Text>
                  </Title>
                </View>
          
                
                  <View >
                    <TouchableOpacity
                      onPress={ () => {togglemethod() }
                      }
                      centered ={true}
                      >
                          <MaterialCommunityIcons
                        name={isDartheme?'moon-waning-crescent': 'white-balance-sunny'}
                        size = {25}
                        color= "blue"
                        style={{paddingLeft: 110, paddingBottom:5, width: '200%'}}
                        > 
                      <Switch
                        value={isDartheme}
                        color="red"
                        style={{paddingLeft: 8}}
                      />
                      </MaterialCommunityIcons>
                    </TouchableOpacity>
                  </View>
                </View>
                  
              ),
            }}
          />
        </Stack.Navigator>
      </NavigationContainer>
    </> 
  );
};

export default App;

【问题讨论】:

  • 从 react-native 而不是 react-native-gesture-handler 导入 TouchableOpacity
  • 问题依然存在。
  • 我什至编辑了这个问题。只是其他人不认为你。我仍然无法解决这个问题。
  • 但是你有效地点击了什么,开关?那你应该在switch里面处理,用onValueChange的方法,可以试试吗?或者尝试移除开关,看看它是否正常工作。
  • 是的,修改为喜欢这个onValueChange = {() =&gt; console.log("pressed!")} 只是为了看到“按下!”在控制台中输出,但没有输出。现在我更多地认为问题出在堆栈中。请帮帮我。

标签: react-native react-native-android react-native-navigation touchableopacity react-native-paper


【解决方案1】:

您调用的方法有错别字,请将 onPress 从 togglemlethod 更改为 togglemethod

【讨论】:

  • 我仍然面临这个问题,是的,确实有一个错字。我更正了,但仍然没有启动。你试过自己运行它吗?
  • 我什至编辑了这个问题。只是其他人不要像你一样假设。我仍然无法解决这个问题。
【解决方案2】:

这是代码的工作版本,

  1. 不应将 Switch 放置在会导致错误的图标内
  2. 您可以使用开关本身来触发切换,

您必须根据需要更改样式,但这在 android 上运行良好。

 <Stack.Navigator screenOptions={{ headerTitleAlign: 'center' }}>
        <Stack.Screen
          name="Home"
          component={HomeScreen}
          options={{
            headerTitle: (props) => (
              <View style={{ flexDirection: 'row' }}>
                <View>
                  <Title style={{ paddingLeft: 130 }}>
                    <Text>Home</Text>
                  </Title>
                </View>

                <View>
                  <TouchableOpacity
                    onPress={() => {
                      togglemethod();
                    }}
                    centered={true}
                    style={{ flexDirection: 'row',alignItems:'center' }}>
                    <Switch
                      value={isDartheme}
                      color="red"
                      onValueChange={() => togglemethod()}
                    />
                    <MaterialCommunityIcons
                      name={
                        isDartheme
                          ? 'moon-waning-crescent'
                          : 'white-balance-sunny'
                      }
                      size={25}
                      color="blue"
                      style={{
                        paddingBottom: 5,
                      }}></MaterialCommunityIcons>
                  </TouchableOpacity>
                </View>
              </View>
            ),
          }}
        />
      </Stack.Navigator>

【讨论】:

  • 我在 Android 上进行了测试,它运行良好,您是否还有其他未在此处提及的包装器?
  • 组件component={Home} 包含执行某些搜索的代码。但它如何影响我不明白的开关。
  • 这里是 Home 组件代码pastebin.com/jCw6avhW
  • 不是您的导航器或 App.js 的主组件
  • 我贴在这里[pastebin.com/F6gvASxZ]看看
猜你喜欢
  • 2018-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-14
  • 2021-11-27
  • 2021-12-21
  • 2018-07-02
  • 1970-01-01
相关资源
最近更新 更多