2016-01-06 13 views
19

Uygulamamdaki bir düğmenin stiline basıldığında değişiklik yapılması istiyorum. Bunu yapmanın en iyi yolu nedir? BöyleDüğme stili üzerinde değişiklik yap React Native

+0

kullanıcı bastırarak değilken sadece basılı edilirken değiştirmek ve geri değiştirmeye ihtiyacınız var mı:

import React from 'react'; import { TouchableHighlight, Text, Alert, StyleSheet } from 'react-native'; export default class TouchableButton extends React.Component { constructor(props) { super(props); this.state = { pressed: false } } render() { return ( <TouchableHighlight onPress={ ()=> { // Alert.alert( // 'You clicked this button', // 'Hello World!', // [ // {text: 'Ask me later', onPress:() => console.log('Ask me later pressed')}, // {text: 'Cancel', onPress:() => console.log('Cancel Pressed'), style: 'cancel'}, // {text: 'OK', onPress:() => console.log('OK Pressed')}, // ] //) } } style={[styles.button, this.state.pressed ? {backgroundColor: 'green'} : {}]} onHideUnderlay={()=>{this.setState({pressed: false})}} onShowUnderlay={()=>{this.setState({pressed: true})}}> <Text>Button</Text> </TouchableHighlight> ); } } const styles = StyleSheet.create({ button: { padding: 10, borderColor: 'blue', borderWidth: 1, borderRadius: 5 }, }); 

+0

Kullanıcıya basmadığı zaman geri dönmek için. – domi91c

cevap

19

kullanın TouchableHighlight. İşte bir örnek

:

enter image description here

'use strict'; 

import React, { 
    Component, 
    StyleSheet, 
    PropTypes, 
    View, 
    Text, 
    TouchableHighlight, 
} from 'react-native'; 

export default class Home extends Component { 
    constructor(props) { 
    super(props); 
    this.state = { pressStatus: false }; 
    } 
    _onHideUnderlay(){ 
    this.setState({ pressStatus: false }); 
    } 
    _onShowUnderlay(){ 
    this.setState({ pressStatus: true }); 
    } 
    render(){ 
    return (
     <View style={styles.container}> 
     <TouchableHighlight 
      activeOpacity={1} 
      style={ this.state.pressStatus ? styles.buttonPress : styles.button } 
      onHideUnderlay={this._onHideUnderlay.bind(this)} 
      onShowUnderlay={this._onShowUnderlay.bind(this)} 
     > 
      <Text style={ this.state.pressStatus ? styles.welcomePress : styles.welcome }>{this.props.text}</Text> 
     </TouchableHighlight> 
     </View> 
    ) 
    } 
} 

Home.propTypes = { 
    text: PropTypes.string.isRequired, 
}; 

const styles = StyleSheet.create({ 
    container: { 
    flex: 1, 
    justifyContent: 'center', 
    alignItems: 'center', 
    backgroundColor: '#F5FCFF', 
    }, 
    welcome: { 
    fontSize: 20, 
    textAlign: 'center', 
    margin: 10, 
    color: '#000066' 
    }, 
    welcomePress: { 
    fontSize: 20, 
    textAlign: 'center', 
    margin: 10, 
    color: '#ffffff' 
    }, 
    button: { 
    borderColor: '#000066', 
    borderWidth: 1, 
    borderRadius: 10, 
    }, 
    buttonPress: { 
    borderColor: '#000066', 
    backgroundColor: '#000066', 
    borderWidth: 1, 
    borderRadius: 10, 
    }, 
}); 
+0

Bu gerçekten yararlı! Ve 'onHideUnderlay' &' onShowUnderlay' yöntemleri 'onPress' ile çakışma değildir. Bu biraz kafamı karıştırdı. –

+2

Sadece '' 'ın çalışması için bir' onPress 'işleyicisi içermesi gereken bir not (React Native v0.34'ten itibaren) – rnevius

+0

'onHideUnderlay' &' onShowUnderlay' hakkında bir şey bilmiyordum! Böyle bir şeyi nasıl başaracağınızı öğrenmek için sonsuza kadar googling oldu. Teşekkür ederim! – gregblass

2

kullanım şey: Gerçekten olursa olsun nasıl uygularım değildir

render() { 
    var _class = "button"; 
    var _class.concat(this.state.onClicked ? "-pressed" : "-normal") ; 
    return (
     <div> 
      <button 
       onClick={this.handlerButtonOnClick} 
       className={_class}>Press me !</button> 
     </div> 
    ); 
    } 

: Harici CSS kullanırsanız

class A extends React.Component { 
    constructor(props){ 
    super(props); 
    this.state = { 
     onClicked: false 
    } 
    this.handlerButtonOnClick = this.handlerButtonOnClick.bind(this); 
    } 
    handlerButtonOnClick(){ 
    this.setState({ 
     onClicked: true 
    }); 
    } 
    render() { 
    var _style; 
    if (this.state.onClicked){ // clicked button style 
     _style = { 
      color: "red" 
     } 
    } 
    else{ // default button style 
     _style = { 
      color: "blue" 
     } 
    } 
    return (
     <div> 
      <button 
       onClick={this.handlerButtonOnClick} 
       style={_style}>Press me !</button> 
     </div> 
    ); 
    } 
} 

, stil özelliğinin yerine SinifAdi kullanabilirsiniz senin CSS. Gözlerinizi "handlerButtonOnClick" yönteminde tutun.

Durum değiştiğinde, bileşen yeniden oluşturulur ("render" yöntemi yeniden çağrılır).

İyi şanslar;)

+0

tepki ana makinesinde bir düğme bileşeni yok. Diğerleri arasında touchableHighlight, touchableNativeFeedback kullanır. – Nico

+0

problem, tepki-native ile ilgili değil ... problem, bileşen üzerindeki stilleri nasıl ve ne zaman uygulayacağını anlamaktır. Akış her zaman aynıdır: handleEvent -> handler -> setState -> render. Cevabım bu akışla ilgili, tepki veren öğeler hakkında değil. –

3

pervane kullanın

? Veya dokunduğunda onu değiştirmeli ve dokunma durduktan sonra değişen durumu mu tutmalısınız?
+0

'underlayColor ', hafif bir opaklıkla görüntülenecektir. 'ActiveOpacity' öğesini 0 olarak ayarlamak, basılmamış duruma uygulanan stilleri tamamen yok sayar. –