index.uts 924 字节
Newer Older
1
import TimePicker from "android.widget.TimePicker"
shutao-dc's avatar
shutao-dc 已提交
2 3

export class NativeTimePicker {
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, hour: number, minute: number) {
shutao-dc's avatar
shutao-dc 已提交
7
		this.$element = element;
8
		this.bindView(hour, minute);
shutao-dc's avatar
shutao-dc 已提交
9 10 11 12
	}

	picker : TimePicker | null = null;
	bindView(hour: number, minute: number) {
13 14 15 16
		this.picker = new TimePicker(this.$element.getAndroidActivity()!);  //构建原生view
		this.$element.bindAndroidView(this.picker!);
		this.setHour(hour)
		this.setMinute(minute)
shutao-dc's avatar
shutao-dc 已提交
17 18
		this.picker?.setOnTimeChangedListener((_, hourOfDay, minute) => {
			const detail = {"hour": hourOfDay, "minute": minute}
shutao-dc's avatar
shutao-dc 已提交
19
			const event = new UniNativeViewEvent("timechanged", detail)
20
			this.$element.dispatchEvent(event)
shutao-dc's avatar
shutao-dc 已提交
21 22 23 24 25 26
		})
	}

	setHour(hour: number) {
		this.picker?.setHour(hour.toInt())
	}
shutao-dc's avatar
shutao-dc 已提交
27

shutao-dc's avatar
shutao-dc 已提交
28 29 30
	setMinute(minute: number) {
		this.picker?.setMinute(minute.toInt())
	}
31
}