私のアプリのボタンのスタイルを変更したいときに押してください。これを行う最善の方法は何ですか?Reactネイティブのプレス上のボタンスタイルを変更する
答えて
使用TouchableHighlight
。ここで
例:
'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,
},
});
使用のようなもの:
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>
);
}
}
あなたは、外部CSSを使用する場合は、スタイルプロパティの代わりにクラス名を使用することができます。
render() {
var _class = "button";
var _class.concat(this.state.onClicked ? "-pressed" : "-normal") ;
return (
<div>
<button
onClick={this.handlerButtonOnClick}
className={_class}>Press me !</button>
</div>
);
}
それは本当に問題であなたが適用されますどのようにしませんあなたのCSS。 "handlerButtonOnClick"メソッドに注目してください。
状態が変化すると、コンポーネントが再描画されます(「レンダリング」メソッドが再度呼び出されます)。
幸運;)
react-nativeにはボタンコンポーネントがありません。 touchableHighlight、touchableNativeFeedbackを使用します。 – Nico
問題は反応ネイティブかどうかについて問題ではありません...問題は、どのように&いつ反応がコンポーネントにスタイルを適用するかを理解しようとしています。 フローは常に同じです:handleEvent - > handler - > setState - > render。 私の答えはこの流れです。反応ネイティブではありません –
これはES6でBesart Hoxhajの答えです。私がこれに答えたとき、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
},
});
小道具を使用します
underlayColor
<TouchableHighlight style={styles.btn} underlayColor={'gray'} />
https://facebook.github.io/react-native/docs/touchablehighlight.html
'underlayColor'はわずかな不透明度で表示されます。 'activeOpacity'を0に設定すると、非圧縮状態に適用されるスタイルは完全に無視されます。 –
- 1. FileUploadコントロールのボタンスタイルを変更
- 2. WPF:プレス上でボタンのキャンバスを変更する方法は?
- 3. プレス上のボタン機能を変更しますか?
- 4. Silverlightで実行時のボタンスタイルを変更
- 5. Win32のオンザフライでボタンスタイルを変更
- 6. gRPC on Reactネイティブ
- 7. Reactネイティブ移動ナビゲータのテキスト
- 8. ReactネイティブFlexboxの100%幅
- 9. プレス上のアニメーションhtml5ボタン
- 10. ReactネイティブWebview get html
- 11. Picker.ItemをReactネイティブにリストするバインド・ピッカー
- 12. PubNubをReactネイティブで使用する
- 13. ReactネイティブAndroidアプリでWebワーカーを作成
- 14. ボタンスタイル
- 15. React Native:ステータスバーのアイコンの色を変更する方法
- 16. Html5ネイティブdatepicker、変更時
- 17. ReactネイティブFBSDKのAccessTokenに関する問題
- 18. Reactネイティブfetch()が動作しない
- 19. ReactネイティブonActivityResultが機能しない
- 20. ボタンスタイルのEventToCommand
- 21. Reactネイティブの新しい小道具を受け取る、componentWillReceiveProps
- 22. 別のボタンスタイルからボタンスタイルへのバインドを行いたい
- 23. vb.netのプレス/リリースキー
- 24. Silverlightボタンスタイル
- 25. HTML、CSS、Jqueryのボタンスタイル
- 26. setNativePropsテキストコンポーネントの値を変更するネイティブ直接操作
- 27. ボタンスタイルの問題を解決する
- 28. Reactネイティブのコンポーネントへのパラメータの受け渡し
- 29. React Native - AppDelegate(iOS)のネイティブからJavaScriptへのイベントの送信
- 30. Java(ネイティブ)印刷ダイアログ - アイコンを変更
を、あなたはそれだけで押している間、それは変更する必要があり、ユーザーはそれを押していないときに戻って変更するのですか?タッチしたときにそれを変更し、タッチが止まった後に変更された状態を維持する必要がありますか? –
ユーザーが押していないときに元に戻す。 – domi91c