Mi piacerebbe che lo stile di un pulsante nella mia app cambi quando viene premuto. Qual è il modo migliore per farlo?Cambia stile pulsante alla pressione in React Native
risposta
Usa TouchableHighlight
.
Ecco un esempio:
'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,
},
});
Questo è veramente utile! E i metodi 'onHideUnderlay' e' onShowUnderlay' non sono in conflitto con 'onPress'. Questo mi fa un po 'confondere un po'. –
Solo una nota che '
Non sapeva di' onHideUnderlay' e 'onShowUnderlay'! Era googling per sempre per scoprire come realizzare qualcosa di simile. Grazie! – gregblass
uso qualcosa di simile:
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>
);
}
}
Se si utilizza un CSS esterno, è possibile utilizzare className al posto di proprietà di stile:
render() {
var _class = "button";
var _class.concat(this.state.onClicked ? "-pressed" : "-normal") ;
return (
<div>
<button
onClick={this.handlerButtonOnClick}
className={_class}>Press me !</button>
</div>
);
}
Non importa come richiederlo il tuo CSS. Tieni d'occhio il metodo "handlerButtonOnClick".
Quando lo stato cambia, il componente viene rieseguito (il metodo "render" viene richiamato di nuovo).
Buona fortuna;)
react-native non ha un componente pulsante. Usa touchableHighlight, touchableNativeFeedback tra gli altri. – Nico
il problema non riguarda la reazione nativa o meno ... il problema sta per capire come e quando reagire applicare gli stili sul componente. Il flusso è sempre lo stesso: handleEvent -> handler -> setState -> render. La mia risposta riguarda questo flusso, non circa la risposta nativa –
Questo è risposta Besart Hoxhaj s' in ES6. Quando rispondo a questo, React Native è 0,34.
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
},
});
Utilizzare il prop:
underlayColor
<TouchableHighlight style={styles.btn} underlayColor={'gray'} />
https://facebook.github.io/react-native/docs/touchablehighlight.html
'underlayColor'sarà visualizzato con una leggera opacità. E l'impostazione di 'activeOpacity' su 0 ignorerà completamente tutti gli stili applicati allo stato non premuto. –
- 1. Cambia Colore pulsante onPress (attiva/disattiva funzionalità) React Native
- 2. Cambia visualizzazione in Navigator in React Native
- 3. Come modificare lo stile del segnaposto TextInput in React Native?
- 4. Larghezza schermo in React Native
- 5. Qual è l'unità di stile predefinita in React Native?
- 6. zIndex in React Native
- 7. setTimeout in React Native
- 8. React Native Android & Genymotion
- 9. Come implementare il pulsante di opzione in React Native
- 10. Gestione del pulsante Indietro in React Native, Navigator su Android
- 11. React-native android Styling textInput
- 12. Scorrere fino alla fine di ScrollView in React Native
- 13. Bind Picker alla lista di Picker.Item in React Native
- 14. Nuovo progetto React Native con versione precedente di react native
- 15. Visualizzazione modale in React Native
- 16. Animazione backgroundColor in React Native
- 17. Ridimensionamento immagine in React Native
- 18. Display: Equivalente in linea in React Native
- 19. React Native - initialProperties Android
- 20. React Native Touchable disabilita ScrollView
- 21. Absolute e Flexbox in React Native
- 22. Elimina tastiera React-Native
- 23. React Native Infinite Scroll
- 24. React Native per Mac
- 25. React Native with Unity
- 26. React Native Linking SMS
- 27. Chiavi ListView native React
- 28. Accedere alla variabile statica di un'altra classe nell'app React-Native?
- 29. Larghezza 100% in React Native Flexbox
- 30. Collegamento della mappa in React Native
Avete bisogno per cambiare mentre viene premuto solo, e cambiare di nuovo quando l'utente non è pressante vero? O hai bisogno di cambiarlo quando viene toccato e mantenere lo stato modificato dopo che il tocco è stato fermato? –
Per tornare indietro quando l'utente non lo sta premendo. – domi91c