react native button 按钮
2016-11-26 22:29
239 查看
用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, } });
相关文章推荐
- React Native入门(五)之使用输入框TextInput,按钮Button搭建登录界面
- react native RadioButton(单选按钮)
- React-Native中使用验证码倒计时的按钮实例代码
- React Native Button实现
- React Native组件之Button
- React-native第一课,Button的添加
- React Native Touchable(按钮) onPress 事件系列总结
- React-Native 之 GD (十七)小时风云榜按钮处理
- RN开源UI组件之react-native-button 使用详解
- 点击Android按钮跳转到React-native指定界面
- React Native Button使用
- React Native android 按钮的波纹效果
- react native Button 使用详解
- react-native-vector-icons的简单使用,图片,按钮,标签视图,导航条
- React Native按钮的实现
- React Native布局实践:开发京东客户端首页(四)——首页功能按钮及控件封装
- React-Native 自定义Button 获取远程数据
- React Native 自定义圆角button的封装
- react-native组件Button封装与传值
- React Native按钮详解|Touchable系列组件使用详解