react native Button 使用详解
2017-04-10 10:17
666 查看
Button其实就是 Touchable(TouchableNativeFeedback、TouchableOpacity)和Text封装而来,下面是Button的部分源码:
由此可见android是TouchableNativeFeedback ,ios是TouchableOpacity,android下title默认大写。
属性:
title:Button显示的文本。
accessibilityLabel:据说这个是用于盲文的,读屏器软件可能会读取这一内容(由于没有相关设备未做考证,在这里也请教下大家。)。
color:ios表示字体的颜色,android表示背景的颜色(莫名其妙啊,不知道为什么这样设计)。
disabled:是否可用,如果为true,禁用此组件的所有交互。
onPress:点击触发函数。
demo:
效果图:
github下载地址
render() { const { accessibilityLabel, color, onPress, title, disabled, } = this.props; const buttonStyles = [styles.button]; const textStyles = [styles.text]; const Touchable = Platform.OS === 'android' ? TouchableNativeFeedback : TouchableOpacity; if (color && Platform.OS === 'ios') { textStyles.push({color: color}); } else if (color) { buttonStyles.push({backgroundColor: color}); } if (disabled) { buttonStyles.push(styles.buttonDisabled); textStyles.push(styles.textDisabled); } invariant( typeof title === 'string', 'The title prop of a Button must be a string', ); const formattedTitle = Platform.OS === 'android' ? title.toUpperCase() : title; return ( <Touchable accessibilityComponentType="button" accessibilityLabel={accessibilityLabel} accessibilityTraits={['button']} disabled={disabled} onPress={onPress}> <View style={buttonStyles}> <Text style={textStyles}>{formattedTitle}</Text> </View> </Touchable> ); }
由此可见android是TouchableNativeFeedback ,ios是TouchableOpacity,android下title默认大写。
属性:
title:Button显示的文本。
accessibilityLabel:据说这个是用于盲文的,读屏器软件可能会读取这一内容(由于没有相关设备未做考证,在这里也请教下大家。)。
color:ios表示字体的颜色,android表示背景的颜色(莫名其妙啊,不知道为什么这样设计)。
disabled:是否可用,如果为true,禁用此组件的所有交互。
onPress:点击触发函数。
demo:
import React, {Component} from 'react'; import { StyleSheet, View, Button, ToastAndroid, } from 'react-native'; export default class ButtonDemo extends Component { render() { return ( <View style={{flex:1}}> <Button title='默认Button' accessibilityLabel='accessibilityLabel'/> <Button title='color设置为红色' color='red' /> <Button title='禁用' disabled={true} onPress={()=>{ ToastAndroid.show('点我了'); }}/> <Button title='禁用' onPress={()=>{ ToastAndroid.show('点我了',ToastAndroid.SHORT); }}/> </View> ); } }
效果图:
github下载地址
相关文章推荐
- RN开源UI组件之react-native-button 使用详解
- RN开源UI组件之react-native-button 使用详解
- React-Native 组件之 Modal的使用详解
- react_native_router_flux_使用详解(二)
- react native Picker 使用详解
- react native ActionSheetIOS 使用详解
- react native FlatList使用详解以及上拉刷新下拉加载带可运行demo
- react native DatePickerIOS 使用详解
- ReactNative Image组件使用详解
- React Native react-navigation 导航使用详解
- react native Slider 使用详解
- react native Switch使用详解
- react native AppState 使用详解
- ReactNative SectionList使用详解
- React Native入门(五)之使用输入框TextInput,按钮Button搭建登录界面
- react native ScrollView 使用详解
- react-native-router-flux 使用详解(二)
- react native ActivityIndicator使用详解
- react native Alert AlertIOS 使用详解
- react native Text 使用详解