SwitchAndroid

标准的 Android 双态切换组件

属性

disable bool

如果为 true,则该组件不能进行交互。

onValueChange function

当值发生变化时调用新的值。

testID string

用于在端到端测试中查找此视图。

value bool

开关的布尔值。

例子

  1. 'use strict';
  2. var React = require('React');
  3. var SwitchAndroid = require('SwitchAndroid');
  4. var Text = require('Text');
  5. var UIExplorerBlock = require('UIExplorerBlock');
  6. var UIExplorerPage = require('UIExplorerPage');
  7. var SwitchAndroidExample = React.createClass({
  8. statics: {
  9. title: '<SwitchAndroid>',
  10. description: 'Standard Android two-state toggle component'
  11. },
  12. getInitialState : function() {
  13. return {
  14. trueSwitchIsOn: true,
  15. falseSwitchIsOn: false,
  16. colorTrueSwitchIsOn: true,
  17. colorFalseSwitchIsOn: false,
  18. eventSwitchIsOn: false,
  19. };
  20. },
  21. render: function() {
  22. return (
  23. <UIExplorerPage title="<SwitchAndroid>">
  24. <UIExplorerBlock title="Switches can be set to true or false">
  25. <SwitchAndroid
  26. onValueChange={(value) => this.setState({falseSwitchIsOn: value})}
  27. style={{marginBottom: 10}}
  28. value={this.state.falseSwitchIsOn} />
  29. <SwitchAndroid
  30. onValueChange={(value) => this.setState({trueSwitchIsOn: value})}
  31. value={this.state.trueSwitchIsOn} />
  32. </UIExplorerBlock>
  33. <UIExplorerBlock title="Switches can be disabled">
  34. <SwitchAndroid
  35. disabled={true}
  36. style={{marginBottom: 10}}
  37. value={true} />
  38. <SwitchAndroid
  39. disabled={true}
  40. value={false} />
  41. </UIExplorerBlock>
  42. <UIExplorerBlock title="Change events can be detected">
  43. <SwitchAndroid
  44. onValueChange={(value) => this.setState({eventSwitchIsOn: value})}
  45. style={{marginBottom: 10}}
  46. value={this.state.eventSwitchIsOn} />
  47. <SwitchAndroid
  48. onValueChange={(value) => this.setState({eventSwitchIsOn: value})}
  49. style={{marginBottom: 10}}
  50. value={this.state.eventSwitchIsOn} />
  51. <Text>{this.state.eventSwitchIsOn ? "On" : "Off"}</Text>
  52. </UIExplorerBlock>
  53. <UIExplorerBlock title="Switches are controlled components">
  54. <SwitchAndroid />
  55. <SwitchAndroid value={true} />
  56. </UIExplorerBlock>
  57. </UIExplorerPage>
  58. );
  59. }
  60. });
  61. module.exports = SwitchAndroidExample;