1# 单选框(Radio) 2 3 4Radio是单选框组件,通常用于提供相应的用户交互选择项,同一组的Radio中只有一个可以被选中。具体用法请参考[Radio](../reference/arkui-ts/ts-basic-components-radio.md)。 5 6 7## 创建单选框 8 9Radio通过调用接口来创建,接口调用形式如下: 10 11 12```ts 13Radio(options: {value: string, group: string}) 14``` 15 16其中,value是单选框的名称,group是单选框的所属群组名称。checked属性可以设置单选框的状态,状态分别为false和true,设置为true时表示单选框被选中。 17 18Radio支持设置选中状态和非选中状态的样式,不支持自定义形状。 19 20```ts 21Radio({ value: 'Radio1', group: 'radioGroup' }) 22 .checked(false) 23Radio({ value: 'Radio2', group: 'radioGroup' }) 24 .checked(true) 25``` 26 27 28 29 30 31## 添加事件 32 33除支持[通用事件](../reference/arkui-ts/ts-universal-events-click.md)外,Radio还用于选中后触发某些操作,可以绑定onChange事件来响应选中操作后的自定义行为。 34 35 36 37```ts 38 Radio({ value: 'Radio1', group: 'radioGroup' }) 39 .onChange((isChecked: boolean) => { 40 if(isChecked) { 41 //需要执行的操作 42 } 43 }) 44 Radio({ value: 'Radio2', group: 'radioGroup' }) 45 .onChange((isChecked: boolean) => { 46 if(isChecked) { 47 //需要执行的操作 48 } 49 }) 50``` 51 52 53## 场景示例 54 55通过点击Radio切换声音模式。 56 57 58```ts 59// xxx.ets 60import promptAction from '@ohos.promptAction'; 61@Entry 62@Component 63struct RadioExample { 64 @State Rst:promptAction.ShowToastOptions = {'message': 'Ringing mode.'} 65 @State Vst:promptAction.ShowToastOptions = {'message': 'Vibration mode.'} 66 @State Sst:promptAction.ShowToastOptions = {'message': 'Silent mode.'} 67 build() { 68 Row() { 69 Column() { 70 Radio({ value: 'Radio1', group: 'radioGroup' }).checked(true) 71 .height(50) 72 .width(50) 73 .onChange((isChecked: boolean) => { 74 if(isChecked) { 75 // 切换为响铃模式 76 promptAction.showToast(this.Rst) 77 } 78 }) 79 Text('Ringing') 80 } 81 Column() { 82 Radio({ value: 'Radio2', group: 'radioGroup' }) 83 .height(50) 84 .width(50) 85 .onChange((isChecked: boolean) => { 86 if(isChecked) { 87 // 切换为振动模式 88 promptAction.showToast(this.Vst) 89 } 90 }) 91 Text('Vibration') 92 } 93 Column() { 94 Radio({ value: 'Radio3', group: 'radioGroup' }) 95 .height(50) 96 .width(50) 97 .onChange((isChecked: boolean) => { 98 if(isChecked) { 99 // 切换为静音模式 100 promptAction.showToast(this.Sst) 101 } 102 }) 103 Text('Silent') 104 } 105 }.height('100%').width('100%').justifyContent(FlexAlign.Center) 106 } 107} 108``` 109 110 111 112