index.uts 1.0 KB
Newer Older
shutao-dc's avatar
shutao-dc 已提交
1 2 3
import { Button } from "android.widget"

export class NativeButton {
shutao-dc's avatar
shutao-dc 已提交
4
	$element : UniNativeViewElement;
shutao-dc's avatar
shutao-dc 已提交
5

shutao-dc's avatar
shutao-dc 已提交
6
	constructor(element : UniNativeViewElement) {
shutao-dc's avatar
shutao-dc 已提交
7
		this.$element = element;
8
		this.bindView();
shutao-dc's avatar
shutao-dc 已提交
9 10 11 12 13
	}

	button : Button | null = null;
	bindView() {
    //通过UniElement.getAndroidActivity()获取android平台activity 用于创建view的上下文
14
		this.button = new Button(this.$element.getAndroidActivity()!);  //构建原生view
shutao-dc's avatar
shutao-dc 已提交
15 16 17 18 19
    //限制原生Button 文案描述不自动大写
    this.button?.setAllCaps(false)
    //监听原生Button点击事件
    this.button?.setOnClickListener(_ => {
    	const detail = {}
shutao-dc's avatar
shutao-dc 已提交
20 21
      //构建自定义UniNativeViewEvent返回对象
    	const event = new UniNativeViewEvent("customClick", detail)
shutao-dc's avatar
shutao-dc 已提交
22
      //响应分发原生Button的点击事件
23
    	this.$element.dispatchEvent(event)
shutao-dc's avatar
shutao-dc 已提交
24
    })
shutao-dc's avatar
shutao-dc 已提交
25
    //UniNativeViewEvent 绑定 安卓原生view
26
		this.$element.bindAndroidView(button!);
shutao-dc's avatar
shutao-dc 已提交
27 28 29 30 31 32 33 34
	}

	updateText(text: string) {
    //更新原生Button 文案描述
		this.button?.setText(text)
	}

}