现象描述
当我们在打开或关闭switch开关时,有时需要弹出对话框让用户确认是否打开或关闭,避免误操作。
例如switch开关默认处于打开状态,在用户关闭开关时,弹出对话框供用户确认。如果用户点击取消,则开关重新置于打开状态;如果用户点击确认,则开关关闭。
实现方法
switch组件的change事件可以用于响应用户打开或关闭开关的操作,通过改变switch组件的checked属性可以实现对开关的控制。
示例代码:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171<template> <div class="container"> <div class="item-container"> <text class="item-title">Default Style</text> <stack> <switch checked="{{checkedValue}}" id="switch" class="switch"></switch> <div class="item-content" style="opacity: 0" "switchTouch"></div> </stack> </div> </div> </template> <script> import prompt from '@system.prompt'; export default { data: { componentName: 'switch', checkedValue: true, }, onInit() { this.$page.setTitleBar({ text: 'switch' }) }, switchTouch: function () { if (this.checkedValue === false) { this.checkedValue = true; } else if (this.checkedValue === true) { console.log(this.checkedValue); var that = this prompt.showDialog({ title: '', message: 'Are you sure you want to disable the switch?', buttons: [ { text: 'OK', color: '#33dd44' }, { text: 'cancel', color: '#33dd44' } ], success: function (data) { console.log("handling callback", data); if (data.index === 0) { that.checkedValue = false; console.log(that.checkedValue); } else { console.log(that.checkedValue); } }, cancel: function () { console.log("cancel"); } }) } } } </script> <style> .container { flex: 1; flex-direction: column; } .item-container { margin-top: 20px; margin-bottom: 30px; flex-direction: column; } .item-title { padding-left: 30px; padding-bottom: 30px; padding-top: 30px; border-bottom-width: 1px; border-color: #bbbbbb; color: #aaaaaa; } .item-content { background-color: #0000cd; border-bottom-width: 1px; border-color: #0000cd; margin-top: 10px; width: 130px; height: 60px; } </style>
实现效果:
欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh
最后
以上就是缥缈小松鼠最近收集整理的关于【快应用】switch组件开关的动态控制的全部内容,更多相关【快应用】switch组件开关内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复