鴻蒙OS 事件

2020-09-18 13:57 更新

事件主要包括手勢(shì)事件和按鍵事件。手勢(shì)事件主要用于智能穿戴等具有觸摸屏的設(shè)備,按鍵事件主要用于智慧屏設(shè)備。

手勢(shì)事件

手勢(shì)表示由單個(gè)或多個(gè)事件識(shí)別的語(yǔ)義動(dòng)作(例如:點(diǎn)擊、拖動(dòng)和長(zhǎng)按)。一個(gè)完整的手勢(shì)也可能由多個(gè)事件組成,對(duì)應(yīng)手勢(shì)的生命周期。JS UI 框架支持的手勢(shì)事件有:

觸摸

  • touchstart:手指觸摸動(dòng)作開始。
  • touchmove:手指觸摸后移動(dòng)。
  • touchcancel:手指觸摸動(dòng)作被打斷,如來(lái)電提醒、彈窗。
  • touchend:手指觸摸動(dòng)作結(jié)束。

點(diǎn)擊

click:用戶快速輕敲屏幕。

長(zhǎng)按

longpress:用戶在相同位置長(zhǎng)時(shí)間保持與屏幕接觸。

具體的使用示例如下:

  1. <!-- xxx.hml -->
  2. <div class="container">
  3. <div class="text-container" onclick="click">
  4. <text class="text-style">{{onclick}}</text>
  5. </div>
  6. <div class="text-container" ontouchstart="touchStart">
  7. <text class="text-style">{{touchStart}}</text>
  8. </div>
  9. <div class="text-container" ontouchmove="touchMove">
  10. <text class="text-style">{{touchMove}}</text>
  11. </div>
  12. <div class="text-container" ontouchend="touchEnd">
  13. <text class="text-style">{{touchEnd}}</text>
  14. </div>
  15. <div class="text-container" ontouchcancel="touchCancel">
  16. <text class="text-style">{{touchCancel}}</text>
  17. </div>
  18. <div class="text-container" onlongpress="longPress">
  19. <text class="text-style">{{onLongPress}}</text>
  20. </div>
  21. </div>

  1. /* xxx.css */
  2. .container {
  3. flex-direction: column;
  4. justify-content: center;
  5. align-items: center;
  6. }
  7. .text-container {
  8. padding-top: 10px;
  9. flex-direction: column;
  10. }
  11. .text-style {
  12. padding-top: 20px;
  13. padding-left: 100px;
  14. width: 750px;
  15. height: 100px;
  16. text-align: center;
  17. font-size: 50px;
  18. color: #ffffff;
  19. background-color: #09ba07;
  20. }

  1. // xxx.js
  2. export default {
  3. data: {
  4. textData: '',
  5. touchStart: 'touchstart',
  6. touchMove: 'touchmove',
  7. touchEnd: 'touchend',
  8. touchCancel: 'touchcancel',
  9. onClick: 'onclick',
  10. onLongPress: 'onlongpress',
  11. },
  12. onInit() {
  13. this.textData = 'initdata';
  14. },
  15. onReady: function () {},
  16. onShow: function () {},
  17. onHide: function () {},
  18. onDestroy: function () {},
  19. touchCancel: function (event) {
  20. this.touchCancel = 'canceled';
  21. },
  22. touchEnd: function(event) {
  23. this.touchEnd = 'ended';
  24. },
  25. touchMove: function(event) {
  26. this.touchMove = 'moved';
  27. },
  28. touchStart: function(event) {
  29. this.touchStart = 'touched';
  30. },
  31. longPress: function() {
  32. this.onLongPress = 'longpressed';
  33. },
  34. click: function() {
  35. this.onClick = 'clicked';
  36. },
  37. }

按鍵事件

按鍵事件是智慧屏上特有的手勢(shì)事件,當(dāng)用戶操作遙控器按鍵時(shí)觸發(fā)。用戶點(diǎn)擊一個(gè)遙控器按鍵,通常會(huì)觸發(fā)兩次 key 事件:先觸發(fā) action 為 0,再觸發(fā) action 為 1,即先觸發(fā)按下事件,再觸發(fā)抬起事件。action 為 2 的場(chǎng)景比較少見,一般為用戶按下按鍵且不松開,此時(shí) repeatCount 將返回次數(shù)。每個(gè)物理按鍵對(duì)應(yīng)各自的按鍵值(keycode)以實(shí)現(xiàn)不同的功能,常用的按鍵值請(qǐng)參考組件通用事件。具體的使用示例如下:

  1. <!-- xxx.hml -->
  2. <div class="card-box">
  3. <div class="content-box">
  4. <text class="content-text" onkey="keyUp" onfocus="focusUp" onblur="blurUp">{{up}}</text>
  5. </div>
  6. <div class="content-box">
  7. <text class="content-text" onkey="keyDown" onfocus="focusDown" onblur="blurDown">{{down}}</text>
  8. </div>
  9. </div>

  1. /* xxx.css */
  2. .card-box {
  3. flex-direction: column;
  4. justify-content: center;
  5. }
  6. .content-box {
  7. align-items: center;
  8. height: 200px;
  9. flex-direction: column;
  10. margin-left: 200px;
  11. margin-right: 200px;
  12. }
  13. .content-text {
  14. font-size: 40px;
  15. text-align: center;
  16. }

  1. // xxx.js
  2. export default {
  3. data: {
  4. up: 'up',
  5. down: 'down',
  6. },
  7. focusUp: function() {
  8. this.up = 'up focused';
  9. },
  10. blurUp: function() {
  11. this.up = 'up';
  12. },
  13. keyUp: function() {
  14. this.up = 'up keyed';
  15. },
  16. focusDown: function() {
  17. this.down = 'down focused';
  18. },
  19. blurDown: function() {
  20. this.down = 'down';
  21. },
  22. keyDown: function() {
  23. this.down = 'down keyed';
  24. },
  25. }

按鍵事件通過獲焦事件向下分發(fā),因此示例中使用了 focus 事件和 blur 事件明確當(dāng)前焦點(diǎn)的位置。點(diǎn)按上下鍵選中 up 或 down 按鍵,即相應(yīng)的 focused 狀態(tài),失去焦點(diǎn)的按鍵恢復(fù)正常的 up 或 down 按鍵文本。按確認(rèn)鍵后該按鍵變?yōu)?keyed 狀態(tài)。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)