index.uts 1.1 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
    //限制原生Button 文案描述不自动大写
    this.button?.setAllCaps(false)
    //监听原生Button点击事件
18
    this.button?.setOnClickListener(_ => {
shutao-dc's avatar
shutao-dc 已提交
19
    	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(this.button!);
shutao-dc's avatar
shutao-dc 已提交
27 28 29 30 31 32 33
	}

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

34 35 36
	destroy(){
		//数据回收
	}
shutao-dc's avatar
shutao-dc 已提交
37
}