【问题标题】:react native: there is an issue while press TouchableOpacity and display the time inside the middle square反应原生:按下 TouchableOpacity 并在中间正方形内显示时间时出现问题
【发布时间】:2020-09-08 11:02:15
【问题描述】:

当我尝试按下 TouchableOpacity 按钮时出现了一些问题,因为它不允许我按下它,而且在我按下它之后,我需要查看时间选择器并根据需要选择一个时间,并且它应该显示在广场内在中间查看。 怎么办呢,因为我现在被堆了。

我添加屏幕图片:

import React, { useState } from 'react';
import { Text, View, Button, Platform, TouchableOpacity } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createMaterialTopTabNavigator } from '@react-navigation/material-top-tabs';
import { useRoute, useNavigation } from '@react-navigation/native';
import DateTimePicker from '@react-native-community/datetimepicker';
import Icon from 'react-native-ionicons';

function NetuneyDigum() {
  const [date, setDate] = useState(new Date());
  const [mode, setMode] = useState('time');
  const [show, setShow] = useState(false);

  const onChange = (event, selectedDate) => {
    setShow(Platform.OS === 'ios');
  };

  const showMode = (currentMode) => {
    setShow(true);
    setMode(currentMode);
  };

  const showTimepicker = () => {
    showMode('time');
  };
  return (
    <>
      <View
        style={{
          flex: 1,
          alignItems: 'center',
          backgroundColor: '#cbced4',
        }}
      >
        <View
          style={{
            paddingTop: 30,
            flexDirection: 'row',
            justifyContent: 'space-evenly',
            paddingRight: 50,
            right: 30,
          }}
        >
          <View
            style={{
              alignItems: 'center',
              flexDirection: 'row',
              justifyContent: 'space-evenly',
              paddingRight: 10,
            }}
          >
            <Text style={{ fontWeight: 'bold', fontSize: 18, color: 'black' }}>
              THE TIME IS :
            </Text>
          </View>
          <View
            style={{
              height: 50,
              width: 100,
              borderRadius: 5,
              borderColor: 'black',
              borderWidth: 2,
              justifyContent: 'center',
              alignItems: 'center',
              flexDirection: 'row',
              // left: 40,
            }}
          >
            <Text
              style={{
                fontSize: 20,
                fontWeight: 'bold',
                color: '#368fc7',
                paddingLeft: 10,
              }}
            >
              {show}
            </Text>
            <TouchableOpacity
              onPress={showTimepicker}
              style={{
                height: 60,
                width: 60,
                borderRadius: 5,
                borderColor: 'black',
                borderWidth: 2,
                left: 100,
                justifyContent: 'center',
                alignItems: 'center',
              }}
            >
              <Icon name="md-time" size={50} color="black" />
            </TouchableOpacity>
          </View>
        </View>

        {show && (
          <DateTimePicker
            testID="dateTimePicker"
            value={date}
            mode={mode}
            is24Hour={true}
            display="default"
            onChange={onChange}
          />
        )}
      </View>
    </>
  );
}

【问题讨论】:

  • 你在代码中的 TouchableOpacity 位置错误和样式设置错误,将 left 属性设置为零并将你的 TouchableOpacity 移到当前父级之外
  • 你能告诉我你在我的代码中修复它的想法吗?
  • 因为它的文字风格很难阅读,你能像我的例子那样写代码吗?
  • 好的,我更改了您代码的相关部分并发送答案

标签: javascript reactjs react-native datepicker react-hooks


【解决方案1】:

您在代码中的 TouchableOpacity 位置错误,样式设置错误,将 left 属性设置为零并将您的 TouchableOpacity 移到当前父级之外

<View
      style={{
        paddingTop: 30,
        flexDirection: 'row',
        justifyContent: 'space-evenly',
        paddingRight: 50,
        right: 30,
      }}
    >
      <View
        style={{
          alignItems: 'center',
          flexDirection: 'row',
          justifyContent: 'space-evenly',
          paddingRight: 10,
        }}
      >
        <Text style={{ fontWeight: 'bold', fontSize: 18, color: 'black' }}>
          THE TIME IS :
        </Text>
      </View>
      <View
        style={{
          height: 50,
          width: 100,
          borderRadius: 5,
          borderColor: 'black',
          borderWidth: 2,
          justifyContent: 'center',
          alignItems: 'center',
          flexDirection: 'row',
          // left: 40,
        }}
      >
        <Text
          style={{
            fontSize: 20,
            fontWeight: 'bold',
            color: '#368fc7',
            paddingLeft: 10,
          }}
        >
          {show}
        </Text>
      </View>
    //move touchable opacity here
    <TouchableOpacity
          onPress={showTimepicker}
          style={{
            height: 60,
            width: 60,
            borderRadius: 5,
            borderColor: 'black',
            borderWidth: 2,
            //left: 100, remove this line
            justifyContent: 'center',
            alignItems: 'center',
          }}
        >
          <Icon name="md-time" size={50} color="black" />
        </TouchableOpacity>
    </View>

【讨论】:

  • 好的,但是在我选择了一段时间后,为什么我在中间的广场上看不到它?
  • DateTimePicker 有一个 onChange 属性,它返回选定的值,在 onChange 方法中你应该 setDate(selectedDate) 并在你的 中写 {date} 而不是 {show}
  • 对不起,我只需要在按下按钮时设置现在的时间,我不需要选择器每次按下时只显示当前时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-08
  • 2021-07-08
  • 2020-10-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-16
  • 1970-01-01
相关资源
最近更新 更多