鸿蒙OS 添加留言区域

2020-09-18 13:41 更新

留言框的功能为:用户输入留言后点击完成,留言区域即显示留言内容;用户点击右侧的删除按钮可删除当前留言内容重新输入。

留言区域由 div、text、input 关联 click 事件实现。开发者可以使用 input 组件实现输入留言的部分,使用 text 组件实现留言完成部分,使用 commentText 的状态标记此时显示的组件(通过 if 属性控制)。在包含文本“完成”和“删除”的 text 组件中关联 click 事件,更新 commentText 状态和 inputValue 的内容。具体的实现示例如下:

  1. <!-- xxx.hml -->
  2. <div class="container">
  3. <div class="left-container">
  4. <text class="comment-title">Comment</text>
  5. <div if="{{!commentText}}">
  6. <input class="comment" value="{{inputValue}}" onchange="updateValue()"></input>
  7. <text class="comment-key" onclick="update" focusable="true">Done</text>
  8. </div>
  9. <div if="{{commentText}}">
  10. <text class="comment-text" focusable="true">{{inputValue}}</text>
  11. <text class="comment-key" onclick="update" focusable="true">Delete</text>
  12. </div>
  13. </div>
  14. </div>

  1. /* xxx.css */
  2. .container {
  3. margin-top: 24px;
  4. background-color: #ffffff;
  5. }
  6. .left-container {
  7. flex-direction: column;
  8. margin-left: 48px;
  9. width: 460px;
  10. }
  11. .comment-title {
  12. font-size: 24px;
  13. color: #1a1a1a;
  14. font-weight: bold;
  15. margin-top: 10px;
  16. margin-bottom: 10px;
  17. }
  18. .comment-key {
  19. width: 74px;
  20. height: 50px;
  21. margin-left: 10px;
  22. font-size: 20px;
  23. color: #1a1a1a;
  24. font-weight: bold;
  25. }
  26. .comment-key:focus {
  27. color: #007dff;
  28. }
  29. .comment-text {
  30. width: 386px;
  31. height: 50px;
  32. text-align: left;
  33. line-height: 35px;
  34. font-size: 20px;
  35. color: #000000;
  36. border-bottom-color: #bcbcbc;
  37. border-bottom-width: 0.5px;
  38. }
  39. .comment {
  40. width: 386px;
  41. height: 50px;
  42. background-color: lightgrey;
  43. }

  1. // xxx.js
  2. export default {
  3. data: {
  4. inputValue: '',
  5. commentText: false,
  6. },
  7. update() {
  8. this.commentText = !this.commentText;
  9. },
  10. updateValue(e) {
  11. this.inputValue = e.text;
  12. },
  13. }
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号