TouchableOpacity组件(绑定事件)

本文介绍如何使用React Native中的TouchableOpacity组件来为应用中的元素添加触摸交互功能。文章通过一个简单的搜索按钮示例展示了如何绑定点击事件,并实现了点击时的视觉反馈效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里写图片描述


代码实现(index.ios.js)

//TouchableOpacity组件(绑定事件)

import React, {Component} from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
   TouchableOpacity
} from 'react-native';

/*
    React Native 提供了3个组件用于给其他没有触摸事件的组件绑定触摸事件
    TouchableOpacity 透明触摸,点击时,组件会出现透明过度效果
    TouchableHighlight  高亮触摸,点击时.组件会出现高亮效果
    TouchableWithoutFeedback 无反馈性触摸,点击时,组件无视觉变化
    需要导入组件
*/

// 组件
var HelloReactNative = React.createClass({
    clickBtn:function () {
        alert("点击搜索");
    },

    render:function () {
        return(
            <View style={styles.container}>
                <View style={styles.flex}>
                    <View style={styles.input}>
                    </View>
                </View>
                <TouchableOpacity style={styles.btn} onPress={this.clickBtn}>
                    <Text style={styles.search}>搜索</Text>
                </TouchableOpacity>
            </View>
        );
    }
});
//样式
var styles = StyleSheet.create({
    container:{
        flexDirection:"row",
        height:45,
        marginTop:25
    },
    flex:{
        flex:1
    },
    input:{
        height:45,
        borderWidth:1,
        marginLeft:5,
        paddingLeft:5,
        borderColor:"#CCC",
        borderRadius:4
    },
    btn:{
        width:55,
        marginLeft:5,
        marginRight:5,
        backgroundColor:"#2873ff",
        height:45,
        justifyContent:"center",
        alignItems:"center"
    },
    search:{
        color:"#FFF",
        fontSize:15,
        fontWeight:"bold"
    }
});


AppRegistry.registerComponent('HelloReactNative', () => HelloReactNative);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小毅哥哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值