react native 自定义Button组件以及事件
1.创建自定义CKButton.js组件类
1 import React,{Component} from 'react'; 2 import { 3 View, 4 StyleSheet, 5 Button, 6 TouchableOpacity, 7 Text 8 } from 'react-native'; 9 10 export default class CKButton extends Component{ 11 constructor(){ 12 super(); 13 } 14 render(){ 15 return( 16 <View style={{justifyContent:'center',alignItems:'center'}}> 17 <Button 18 title="登录" 19 onPress={()=>this._onBtnPress()} 20 color="red" 21 /> 22 <TouchableOpacity 23 style={styles.btnStyle} 24 activeOpacity={0.6} 25 onPress={()=>this._onBtnPress()} 26 onPressIn={()=>this._onBtnPress()}//按下按钮事件 27 onPressOut={()=>this._onBtnPress()}//抬起按钮事件 28 onLongPress={()=>this._onBtnPress()}//长按按钮事件 29 > 30 <Text style={{fontSize:25,color:'green'}}>注册账号</Text> 31 </TouchableOpacity> 32 </View> 33 ) 34 } 35 36 _onBtnPress(){ 37 alert('点我干嘛?') 38 } 39 } 40 41 const styles=StyleSheet.create({ 42 btnStyle:{ 43 width:200, 44 height:40, 45 borderRadius:10, 46 backgroundColor:'red', 47 justifyContent:'center', 48 alignItems:'center' 49 } 50 })
2.App.js 引入自定义组件
/** * Sample React Native App * https://github.com/facebook/react-native * * @format * @flow strict-local */ import React from 'react'; import { SafeAreaView, StyleSheet, ScrollView, View, Text, StatusBar, } from 'react-native'; import { Header, LearnMoreLinks, Colors, DebugInstructions, ReloadInstructions, } from 'react-native/Libraries/NewAppScreen'; import CKButton from './components/CKButton' const App: () => React$Node = () => { return ( <> <StatusBar barStyle="dark-content" /> <SafeAreaView style={styles.mainViewStyle}> <CKButton/> </SafeAreaView> </> ); }; const styles=StyleSheet.create({ mainViewStyle:{ flex:1, backgroundColor:'#fff', } }); export default App;
3.结果如图
分类:
React Native
标签:
React Native
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!