javascript 响应本地touchableOpacity on按下更改输入字段

wgx48brx  于 2023-01-29  发布在  Java
关注(0)|答案(1)|浏览(161)

我怎样改变一个输入框的可触摸不透明度?2再一次改变可触摸的不透明度?

<TouchableOpacity
                   onPress={() => {
                     showGradeField();
                   }}
                   style={styles.roundButton}
                 >
                   <Text>TEST</Text>
 </TouchableOpacity>
8fq7wneg

8fq7wneg1#

创建状态变量isEditable,以存储文本输入是否处于活动状态或TouchableOpacity。当TouchableOpacity处于活动状态时,在按下时切换isEditable
这对于TextInput来说有点困难,因为如果你切换按键,那么当用户按下TextInput编辑文本时,你会切换回Text组件,使其不可编辑。因此,当用户完成编辑时,你切换事件,例如onBluronPressOutonSubmitEditingdemo):

import { useState, useCallback, useRef, useEffect } from 'react';
import {
  Text,
  TextInput,
  View,
  StyleSheet,
  TouchableOpacity,
} from 'react-native';

const showToggleValue = false;

export default function EditableText({
  initialIsEditable,
  initialText,
  containerStyle,
  inputStyle,
  textStyle,
  contentContainerStyle,
}) {
  const [isEditable, setIsEditable] = useState(initialIsEditable);
  const [text, setText] = useState(initialText || 'Typing...');
  const toggle = useCallback(()=>setIsEditable(prev=>!prev),[])
  // when switching to TextInput focus it
  const textInputRef = useRef(null);
  useEffect(()=>{
    // focuses the textinput after TouchableOpacity is pressed
    if(isEditable)
      textInputRef.current?.focus()
  },[isEditable])
  return (
    <View style={[styles.container, containerStyle, inputStyle]}>
      <Text>Current using: {isEditable ? 'TextInput' : 'Text'} component</Text>
      {isEditable ? (
        <View style={[styles.pressableContainer, contentContainerStyle]}>
          <TextInput
            ref={textInputRef}
            onChangeText={setText}
            value={text}
            onBlur={toggle}
            onPressOut={toggle}
            onSubmitEditing={toggle}
            style={[styles.inputStyle, inputStyle]}
          />
        </View>
      ) : (
        <TouchableOpacity
          style={[styles.pressableContainer, contentContainerStyle]}
          onPress={toggle}>
          <Text style={textStyle}>{text}</Text>
        </TouchableOpacity>
      )}
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    width: '100%',
    margin: 5,
    padding: 5,
  },
  pressableContainer: {
    width: '100%',
    borderBottomWidth: 1,
    margin: 5,
    padding: 5,
    backgroundColor: 'pink',
  },
  inputStyle: {
    padding: 5,
    borderWidth: 0,
  },
});

相关问题