【发布时间】:2022-08-14 17:45:10
【问题描述】:
我正在使用提供的 FlatList 显示下面的状态数组。但是,Key 中的 TouchableOpacity 元素对按下没有任何响应。即使我将 TO 更改为按钮或可按下的,问题仍然存在。有没有人有这个问题的解决方案,因为即使使用非常相似的代码,它也从未发生过。
const [keyboard, setKeyboard] = useState([
{char: \'a\', background: \'white\', border: \'black\', text: \'black\'},
.
.
.
{char: \'z\', background: \'white\', border: \'black\', text: \'black\'},
]);
const Key = ({letter, background, border, textColor}) => {
return(
<TouchableOpacity style = {[styles.key, {backgroundColor: {background}}, {borderColor: {border}}]} onPress = {() => console.log({letter})}>
<Text style = {[styles.letter, {color: {textColor}}]}>{letter}</Text>
</TouchableOpacity>
);
};
<FlatList
contentContainerStyle = {styles.keyboard}
data={keyboard}
renderItem= {({item}) => <Key letter={item.char} background={item.background} border={item.border} textColor={item.text}/>}
keyExtractor={(item) => item.char}
numColumns = {6}
/>
款式:
keyboard:{
width: \'100%\',
height: \'40%\',
alignItems: \'center\',
justifyContent: \'center\',
position: \'absolute\',
transform:[{translateY: 420}],
flex: 1,
},
letter:{
fontSize: 25,
fontWeight: \'bold\',
position: \'absolute\',
},
key:{
height: 50,
width: 60,
borderWidth: 3,
borderRadius: 10,
alignItems: \'center\',
justifyContent: \'center\',
},
标签: react-native react-hooks react-native-flatlist touchableopacity