index.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <template>
  2. <view>
  3. </view>
  4. </template>
  5. <script lang="uts">
  6. /**
  7. * 引用 Android 系统库
  8. * [可选实现,按需引入]
  9. */
  10. import TextUtils from 'android.text.TextUtils';
  11. import Button from 'android.widget.Button';
  12. import View from 'android.view.View';
  13. /**
  14. * 引入三方库
  15. * [可选实现,按需引入]
  16. *
  17. * 在 Android 平台引入三方库有以下两种方式:
  18. * 1、[推荐] 通过 仓储 方式引入,将 三方库的依赖信息 配置到 config.json 文件下的 dependencies 字段下。详细配置方式[详见](https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#dependencies)
  19. * 2、直接引入,将 三方库的aar或jar文件 放到libs目录下。更多信息[详见](https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#android%E5%B9%B3%E5%8F%B0%E5%8E%9F%E7%94%9F%E9%85%8D%E7%BD%AE)
  20. *
  21. * 在通过上述任意方式依赖三方库后,使用时需要在文件中 import
  22. * import { LottieAnimationView } from 'com.airbnb.lottie.LottieAnimationView'
  23. */
  24. /**
  25. * UTSAndroid 为平台内置对象,不需要 import 可直接调用其API,[详见](https://uniapp.dcloud.net.cn/uts/utsandroid.html#utsandroid)
  26. */
  27. //原生提供以下属性或方法的实现
  28. export default {
  29. /**
  30. * 组件名称,也就是开发者使用的标签
  31. */
  32. name: "uts-button",
  33. /**
  34. * 组件涉及的事件声明,只有声明过的事件,才能被正常发送
  35. */
  36. emits: ['buttonclick'],
  37. /**
  38. * 属性声明,组件的使用者会传递这些属性值到组件
  39. */
  40. props: {
  41. "buttontext": {
  42. type: String,
  43. default: "点击触发"
  44. }
  45. },
  46. /**
  47. * 组件内部变量声明
  48. */
  49. data() {
  50. return {}
  51. },
  52. /**
  53. * 属性变化监听器实现
  54. */
  55. watch: {
  56. "buttontext": {
  57. /**
  58. * 这里监听属性变化,并进行组件内部更新
  59. */
  60. handler(newValue : string, oldValue : string) {
  61. if (!TextUtils.isEmpty(newValue) && newValue != oldValue) {
  62. this.$el?.setText(newValue);
  63. }
  64. },
  65. immediate: false // 创建时是否通过此方法更新属性,默认值为false
  66. },
  67. },
  68. /**
  69. * 规则:如果没有配置expose,则methods中的方法均对外暴露,如果配置了expose,则以expose的配置为准向外暴露
  70. * ['publicMethod'] 含义为:只有 `publicMethod` 在实例上可用
  71. */
  72. expose: ['doSomething'],
  73. methods: {
  74. /**
  75. * 对外公开的组件方法
  76. *
  77. * uni-app中调用示例:
  78. * this.$refs["组件ref"].doSomething("uts-button");
  79. *
  80. * uni-app x中调用示例:
  81. * 1、引入对应Element
  82. * import { UtsButtonElement(组件名称以upper camel case方式命名 + Element) } from 'uts.sdk.modules.utsComponent(组件目录名称以lower camel case方式命名)';
  83. * 2、(this.$refs["组件ref"] as UtsButtonElement).doSomething("uts-button");
  84. * 或 (uni.getElementById("组件id") as UtsButtonElement).doSomething("uts-button");
  85. */
  86. doSomething(param : string) {
  87. console.log(param);
  88. },
  89. /**
  90. * 内部使用的组件方法
  91. */
  92. privateMethod() {
  93. }
  94. },
  95. /**
  96. * [可选实现] 组件被创建,组件第一个生命周期,
  97. * 在内存中被占用的时候被调用,开发者可以在这里执行一些需要提前执行的初始化逻辑
  98. */
  99. created() {
  100. },
  101. /**
  102. * [可选实现] 对应平台的view载体即将被创建,对应前端beforeMount
  103. */
  104. NVBeforeLoad() {
  105. },
  106. /**
  107. * [必须实现] 创建原生View,必须定义返回值类型
  108. * 开发者需要重点实现这个函数,声明原生组件被创建出来的过程,以及最终生成的原生组件类型
  109. * (Android需要明确知道View类型,需特殊校验)
  110. */
  111. NVLoad() : Button {
  112. let button = new Button($androidContext!);
  113. button.setText("点击触发");
  114. button.setOnClickListener(new ButtonClickListener(this));
  115. return button;
  116. },
  117. /**
  118. * [可选实现] 原生View已创建
  119. */
  120. NVLoaded() {
  121. },
  122. /**
  123. * [可选实现] 原生View布局完成
  124. */
  125. NVLayouted() {
  126. },
  127. /**
  128. * [可选实现] 原生View将释放
  129. */
  130. NVBeforeUnload() {
  131. },
  132. /**
  133. * [可选实现] 原生View已释放,这里可以做释放View之后的操作
  134. */
  135. NVUnloaded() {
  136. },
  137. /**
  138. * [可选实现] 组件销毁
  139. */
  140. unmounted() {
  141. },
  142. /**
  143. * [可选实现] 自定组件布局尺寸,用于告诉排版系统,组件自身需要的宽高
  144. * 一般情况下,组件的宽高应该是由终端系统的排版引擎决定,组件开发者不需要实现此函数
  145. * 但是部分场景下,组件开发者需要自己维护宽高,则需要开发者重写此函数
  146. */
  147. NVMeasure(size : UTSSize) : UTSSize {
  148. // size.width = 300.0.toFloat();
  149. // size.height = 200.0.toFloat();
  150. return size;
  151. }
  152. }
  153. /**
  154. * 定义按钮点击后触发回调的类
  155. * [可选实现]
  156. */
  157. class ButtonClickListener extends View.OnClickListener {
  158. /**
  159. * 如果需要在回调类或者代理类中对组件进行操作,比如调用组件方法,发送事件等,需要在该类中持有组件对应的原生类的对象
  160. * 组件原生类的基类为 UTSComponent,该类是一个泛型类,需要接收一个类型变量,该类型变量就是原生组件的类型
  161. */
  162. private comp : UTSComponent<Button>;
  163. constructor(comp : UTSComponent<Button>) {
  164. super();
  165. this.comp = comp;
  166. }
  167. /**
  168. * 按钮点击回调方法
  169. */
  170. override onClick(v ?: View) {
  171. console.log("按钮被点击");
  172. // 发送事件
  173. this.comp.$emit("buttonclick");
  174. }
  175. }
  176. </script>
  177. <style>
  178. </style>