react native button 按钮
来源:互联网 发布:网络名誉侵权 判决 编辑:程序博客网 时间:2024/04/29 07:43
用react native自定义一个button按钮
button代码,响应按下事件,设置获取disabled状态,设置获取显示文本
import React, { Component } from 'react';import { AppRegistry, StyleSheet, Text, View, TouchableOpacity} from 'react-native';export default class Button extends Component { constructor(props) { super(props); this.state = { disabled: props.disabled, text: props.text, } } onPress_() { if (typeof this.props.onPress === 'function') { this.props.onPress(); } } setDisabled_(yes) { this.setState({disabled: yes}); } isDisabled_() { return this.state.disabled; } setText_(text) { this.setState({text: text}); } text_() { return this.state.text; } render() { return ( <TouchableOpacity style={[styles.button, this.state.disabled && styles.disabled]} onPress={this.onPress_.bind(this)} disabled={this.state.disabled} > <Text style={styles.text}> {this.state.text} </Text> </TouchableOpacity> ) }}styles = StyleSheet.create({ button: { backgroundColor:'#0c3ba9', width: 80, height: 40, justifyContent: 'center', borderRadius: 10, overflow: 'hidden', }, text: { textAlign: 'center', color: '#fff', fontSize: 16, }, disabled: { backgroundColor: '#999999' }})
测试代码
import React, { Component } from 'react';import { AppRegistry, StyleSheet, Text, View, TextInput} from 'react-native';import Button from '../common/Button.js'export default class ButtonTest extends Component { onToggle_() { this.refs.okButton.setDisabled_(!this.refs.okButton.isDisabled_()); } render() { return ( <View style={styles.container}> <Button ref="okButton" text="确定" onPress={()=>{alert(this.refs.okButton.text_())}} /> <Button text="切换状态" onPress={this.onToggle_.bind(this)} /> <TextInput style={styles.textInput} placeholder="改变确定按钮文本" onChangeText={(text) => this.refs.okButton.setText_(text)} /> </View> ); }}const styles = StyleSheet.create({ container: { flex: 1, flexDirection: 'row', justifyContent: 'space-around', alignItems: 'center', backgroundColor: '#F5FCFF', }, textInput: { width: 130, }});
0 0
- react native button 按钮
- React native自定义组件之Button按钮
- React Native Button实现
- react-native Button
- React Native Button使用
- React-Native(三)--Button
- React Native自定义Button
- react -native 自定义按钮
- React-native简单button实现
- react-native-Button-TouchableOpacity
- react-native--万能Button封装
- react native Button 使用详解
- React Native学习七- button
- React Native组件之Button
- React Native入门(五)之使用输入框TextInput,按钮Button搭建登录界面
- react native Back 按钮封装
- react-native 自定义倒计时按钮
- react native 多选按钮
- C语言习题——提示输入3个整数值,分别代表日,月,年
- 十进制整数和小数的转换
- [读书笔记]30 天自制操作系统 day3 进入32bit模式并导入C语言
- maven安装部署
- 带你玩转JavaWed开发之二-CSS从基础到实战
- react native button 按钮
- 编译原理/紫龙书(第2版) 答案 1.6
- Java简易RPC框架学习(一)
- 22.分布式系统基础设施
- POJ3420_Quad Tiling_(找规律_矩阵_快速幂取模)
- hive内部表与外部表区别详细介绍
- 数据库学习笔记
- Android压缩图片到100K以下并保持不失真的高效方法
- [PAT]Basic Level 1005.继续(3n+1)猜想