index.uts 1.0 KB
Newer Older
shutao-dc's avatar
shutao-dc 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
import { Button } from "android.widget"

export class NativeButton {
	$element : UniObjectElement;

	constructor(element : UniObjectElement) {
		this.$element = element;
		bindView();
	}

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

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

}