React-Native 自定義按鈕系列

RN提供了好幾種按鈕,這里介紹最為常用的三種

1:第一種按鈕最原始的Button

常用屬性:

title:按鈕顯示名稱

onPress:按鈕點(diǎn)擊的事件

color:顯示文字的顏色

創(chuàng)建一個(gè)Button
<Button title="Button" 
        onPress={this.actionButton} 
             color={'#aaffaa'}/>

2:第二種按鈕TouchableOpacity,點(diǎn)擊按鈕更改按鈕的透明度

常用屬性:

activeOpacity:設(shè)置點(diǎn)擊的透明度0-1

創(chuàng)建一個(gè)TouchableOpacity
<TouchableOpacity style={styles.touchButton}
                             onPress={() => {
                                 alert('TouchableOpacity')
                           }}>
                    <Text style={styles.touchButtonText}>點(diǎn)擊按鈕1</Text>
                </TouchableOpacity>

3:第三種按鈕TouchableHighlight,點(diǎn)擊按鈕可以更改按鈕的背景色和透明度

常用屬性:

activeOpacity:設(shè)置點(diǎn)擊的透明度0-1

underlayColor:點(diǎn)擊狀態(tài)的顏色

onHideUnderlay:隱藏點(diǎn)擊狀態(tài)時(shí)調(diào)用的方法

onShowUnderlay:顯示點(diǎn)擊狀態(tài)時(shí)調(diào)用的方法

創(chuàng)建一個(gè)TouchableHighlight
<TouchableHighlight activeOpacity={0.9}
                                    underlayColor={'#1aaf00'}
                                    style={[styles.touchButton]}
                                    onPress={() => {
                    alert('TouchableHighlight')
                }}>
                    <Text style={styles.touchButtonText}>點(diǎn)擊按鈕2</Text>
                </TouchableHighlight>

完整的創(chuàng)建三種按鈕:

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

export default class ButtonView extends Component {
    render() {
        return(
            <View style={{
                backgroundColor: '#ffaaaa', 
                flex: 1,
                alignItems: 'center',
                justifyContent: 'center'}}>
                <Button title="Button" 
                        onPress={this.actionButton}
                        color={'#aaffaa'}/>
                <TouchableOpacity style={styles.touchButton} 
                                  onPress={() => {
                    alert('TouchableOpacity')
                }}>
                    <Text style={styles.touchButtonText}>點(diǎn)擊按鈕1</Text>
                </TouchableOpacity>
                <TouchableHighlight activeOpacity={0.9}
                                    underlayColor={'#1aaf00'}
                                    style={[styles.touchButton]}
                                    onPress={() => {
                    alert('TouchableHighlight')
                }}>
                    <Text style={styles.touchButtonText}>點(diǎn)擊按鈕2</Text>
                </TouchableHighlight>
            </View>
        )
    }
    actionButton = () => {
        alert('Button')
    }
}
const styles = StyleSheet.create({
    touchButton: {
        height: 40,
        width: 100,
        borderRadius: 20,
        backgroundColor: '#fa1faa',
        justifyContent: 'center',
        overflow: 'hidden',
    },
    touchButtonText: {
        color: 'white',
        textAlign: 'center',
    }
});

AppRegistry.registerComponent('ButtonView', ()=> ButtonView);

效果展示:

Untitled8.gif
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 尊重版權(quán),未經(jīng)授權(quán)不得轉(zhuǎn)載本文出自:http://www.lxweimin.com/u/ca3943a4172a 在...
    CrazyCodeBoy閱讀 25,935評(píng)論 5 20
  • 在做App開(kāi)發(fā)過(guò)程中離不了的需要用戶交互,說(shuō)到交互,我們首先會(huì)想到的就是按鈕了,在React Native中沒(méi)有專...
    Jason_yuanxd閱讀 931評(píng)論 0 0
  • Touchable系列組件 為了能讓視圖能夠響應(yīng)用的的點(diǎn)擊事件,我們需要借助Touchablexxx組件,來(lái)包裹我...
    基本密碼宋閱讀 5,616評(píng)論 0 0
  • 前言 學(xué)習(xí)本系列內(nèi)容需要具備一定 HTML 開(kāi)發(fā)基礎(chǔ),沒(méi)有基礎(chǔ)的朋友可以先轉(zhuǎn)至 HTML快速入門(mén)(一) 學(xué)習(xí) 本人...
    珍此良辰閱讀 6,422評(píng)論 0 9
  • 一夜春雨,空氣聞起來(lái)好舒爽,忽然想吃上一口冰激凌,約了小情人 把車(chē)停在小區(qū)門(mén)口,隨手翻著書(shū),抬頭就看到小情人——風(fēng)...
    騰歡媽媽閱讀 339評(píng)論 0 0