react native 手搓数字键盘

react native 手搓数字键盘

本文展示了一个React Native实现的数字键盘组件ModalKeyboard。该组件包含以下特点: 使用Modal容器实现从底部滑出的动画效果 键盘布局为3x4的网格结构,包含数字0-9、"重置"和"-"按钮 支持点击数字提交功能,通过onSubmit回调传递按键值 自定义样式包括圆角按钮、背景色和字体大小 "-"按钮使用图片替代文字显示 通过点击模态框外部区域关闭键盘 组件采用响应式设计,使用zero工具函数动态计算布局尺寸,适用于不同屏幕

	import React, { Component } from 'react';
	import { Modal, View, Text, TouchableOpacity, StyleSheet, Image } from 'react-native';
	class ModalKeyboard extends Component {
	  constructor(props) {
	    super(props);
	    this.state = {
	      visible: false,
	    };
	  }
	
	  handlePress = (key) => {
	    this.props.onSubmit(key);
	  };
	  closeKeyboard = () => {
	    this.setState({ visible: false });
	  }
	
	  render() {
	    const { visible } = this.state;
	    const keys = [
	      ['1', '2', '3'],
	      ['4', '5', '6'],
	      ['7', '8', '9'],
	      ['重置', '0', '-'],
	    ];
	
	    return (
	      <Modal
	        animationType="slide"
	        transparent={true}
	        visible={visible}
	        onRequestClose={() => {
	          this.setState({ visible: false });
	        }}
	      >
	        <TouchableOpacity style={styles.centeredView} onPress={this.closeKeyboard}>
	          <TouchableOpacity style={styles.modalView} activeOpacity={1} > 
	            {keys.map((row, rowIndex) => (
	              <View key={rowIndex} style={styles.row}>
	                {row.map((key) => (
	
	                  key === '-' ?
	                    <TouchableOpacity
	                      key={key}
	                      style={[styles.button,{backgroundColor:"#007FFF"}]}
	                      onPress={() => this.handlePress(key)}
	                    >
	                      <Image resizeMode='center' source={require('./../res/jian.png')} style={styles.jian}></Image>
	                    </TouchableOpacity>
	                    :
	                    <TouchableOpacity
	                      key={key}
	                      style={styles.button}
	                      onPress={() => this.handlePress(key)}
	                    >
	                      <Text style={[styles.buttonText, key == '重置'? { fontSize: zero.yFont(24) } : {}]}>{key}</Text>
	                    </TouchableOpacity>
	
	
	
	
	                ))}
	              </View>
	            ))}
	          </TouchableOpacity>
	        </TouchableOpacity>
	      </Modal>
	    );
	  }
	}
	
	const styles = StyleSheet.create({
	  centeredView: {
	    flex: 1,
	    justifyContent: "flex-end",
	    alignItems: 'center',
	  },
	  modalView: {
	    backgroundColor: '#333333',
	    width: '100%',
	    height: zero.yHeight(360),
	    alignItems: 'center',
	    elevation: 5,
	  },
	  row: {
	    flexDirection: 'row',
	  },
	  cardContainer: {
	    marginTop: zero.yHeight(10),
	    marginRight: zero.yWidth(10),
	    // marginHorizontal: zero.yWidth(10),
	    width: zero.yWidth(120),
	    height: zero.yHeight(70),
	    borderRadius: zero.yWidth(10),
	  },
	  button: {
	    marginTop: zero.yHeight(10),
	    marginRight: zero.yWidth(10),
	    width: zero.yWidth(120),
	    height: zero.yHeight(70),
	    alignItems: 'center',
	    justifyContent: 'center',
	    borderRadius: zero.yWidth(10),
	    backgroundColor: '#666',
	
	  },
	  buttonText: {
	    color: "#fff",
	    fontSize: zero.yFont(40),
	  },
	  jian: {
	    width: zero.yWidth(36),
	  }
	});
export default ModalKeyboard;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值