keyboard.uvue 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <template>
  2. <view class="container">
  3. <view class="input-section">
  4. <input id="uni-input-box" class="input-box" type="text" :value="inputValue" placeholder="点击输入框显示键盘" :focus="isFocus" hold-keyboard="true" />
  5. <button class="btn" @click="hideKeyboard">隐藏键盘</button>
  6. </view>
  7. <view class="info-section">
  8. <text class="info-text">键盘高度: {{keyboardHeight}}px</text>
  9. <text class="info-text">键盘状态: {{keyboardStatus}}</text>
  10. </view>
  11. </view>
  12. </template>
  13. <script>
  14. export default {
  15. data() {
  16. return {
  17. inputValue: '',
  18. isFocus: false,
  19. keyboardHeight: 0,
  20. keyboardStatus: '未显示'
  21. }
  22. },
  23. onLoad() {
  24. // 监听键盘高度变化
  25. uni.onKeyboardHeightChange(res => {
  26. this.keyboardHeight = res.height;
  27. this.keyboardStatus = res.height > 0 ? '显示中' : '已隐藏';
  28. });
  29. },
  30. onUnload() {
  31. // 页面卸载时移除监听
  32. uni.offKeyboardHeightChange();
  33. },
  34. methods: {
  35. hideKeyboard() {
  36. uni.hideKeyboard();
  37. }
  38. }
  39. }
  40. </script>
  41. <style>
  42. .container {
  43. padding: 20px;
  44. }
  45. .input-section {
  46. margin-bottom: 20px;
  47. }
  48. .input-box {
  49. width: 100%;
  50. height: 40px;
  51. border: 1px solid #ccc;
  52. border-radius: 4px;
  53. padding: 0 10px;
  54. margin-bottom: 10px;
  55. }
  56. .btn {
  57. background-color: #007AFF;
  58. color: #fff;
  59. }
  60. .info-section {
  61. margin-top: 20px;
  62. }
  63. .info-text {
  64. width: 100%;
  65. margin-bottom: 10px;
  66. font-size: 16px;
  67. color: #333;
  68. }
  69. </style>