element-getnativeview.uvue 3.3 KB
Newer Older
shutao-dc's avatar
shutao-dc 已提交
1 2 3 4 5
<template>
  <view id="view" style="flex: 1;">
    <input id="input" value="input" class="input" />
    <textarea id="textarea" value="textarea" class="textarea" />
    <web-view id="webview" src="/hybrid/html/local.html" class="web-view"></web-view>
6 7 8 9
    <button class="button" type="primary" @click="checkViewNativeView">检测view标签原生View</button>
    <button class="button" type="primary" @click="checkInputNativeView">检测input标签原生View</button>
    <button class="button" type="primary" @click="checkTextareaNativeView">检测textarea标签原生View</button>
    <button class="button" type="primary" @click="checkWebViewNativeView">检测webview标签原生View</button>
shutao-dc's avatar
shutao-dc 已提交
10 11 12 13 14 15 16 17 18 19 20 21
  </view>
</template>

<script>
  import { checkWebViewNativeView, checkInputNativeView, checkTextareaNativeView, checkViewNativeView } from '@/uni_modules/uts-get-native-view';
  export default {
    data() {
      return {

      }
    },
    methods: {
22 23 24 25 26 27
      checkViewNativeView() : boolean {
        var viewName = "ViewGroup"
        // #ifdef APP-IOS
        viewName = "UIView"
        // #endif
        const msg = "检测view组件对应原生"+viewName
shutao-dc's avatar
shutao-dc 已提交
28
        if (checkViewNativeView("view")) {
29
          this.showTip(msg+"成功")
shutao-dc's avatar
shutao-dc 已提交
30 31
          return true
        }
32
        this.showTip(msg+"失败")
shutao-dc's avatar
shutao-dc 已提交
33 34
        return false
      },
35 36 37 38 39 40
      checkInputNativeView() : boolean {
        var viewName = "AppCompatEditText"
        // #ifdef APP-IOS
        viewName = "UITextField"
        // #endif
        const msg = "检测input组件对应原生"+viewName
shutao-dc's avatar
shutao-dc 已提交
41
        if (checkInputNativeView("input")) {
42
          this.showTip(msg+"成功")
shutao-dc's avatar
shutao-dc 已提交
43 44
          return true
        }
45
        this.showTip(msg+"失败")
shutao-dc's avatar
shutao-dc 已提交
46 47
        return false
      },
48
      checkTextareaNativeView() : boolean {
49
        var viewName = "AppCompatEditText"
50
        // #ifdef APP-IOS
51
        viewName = "UITextView"
52 53
        // #endif
        const msg = "检测textarea组件对应原生"+viewName
shutao-dc's avatar
shutao-dc 已提交
54
        if (checkTextareaNativeView("textarea")) {
55
          this.showTip(msg+"成功")
shutao-dc's avatar
shutao-dc 已提交
56 57
          return true
        }
58
        this.showTip(msg+"失败")
shutao-dc's avatar
shutao-dc 已提交
59 60
        return false
      },
61 62 63 64 65 66
      checkWebViewNativeView() : boolean {
        var viewName = "WebView"
        // #ifdef APP-IOS
        viewName = "WKWebView"
        // #endif
        const msg = "检测webview组件对应原生"+viewName
shutao-dc's avatar
shutao-dc 已提交
67
        if (checkWebViewNativeView("webview")) {
68
          this.showTip(msg+"成功")
shutao-dc's avatar
shutao-dc 已提交
69 70
          return true
        }
71
        this.showTip(msg+"失败")
shutao-dc's avatar
shutao-dc 已提交
72
        return false
73 74 75 76 77 78 79
      },
      showTip(title: string) {
        console.log("title==="+title)
        uni.showToast({
          title: title,
          icon: "none"
        })
shutao-dc's avatar
shutao-dc 已提交
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
      }
    }
  }
</script>

<style>
  .input {
    width: 300px;
    height: 40px;
    border-radius: 4px;
    border-width: 1px;
    border-color: black;
    border-style: solid;
    margin: 20px auto;
  }

  .textarea {
    width: 300px;
    height: 80px;
    border-radius: 4px;
    border-width: 1px;
    border-color: black;
    border-style: solid;
    margin: 20px auto;
  }

  .web-view {
    width: 300px;
    height: 120px;
    margin: 20px auto;
    border-radius: 4px;
    border-width: 1px;
    border-color: black;
    border-style: solid;
114 115 116 117
  }

  .button {
    margin: 10px 20px;
shutao-dc's avatar
shutao-dc 已提交
118 119
  }
</style>