sample: 自定义控件; fix: ActivityNotFoundException

This commit is contained in:
hyb1996
2018-10-14 10:33:23 +08:00
parent db3571851c
commit 14b7a7e85e
39 changed files with 615 additions and 220 deletions

View File

@@ -0,0 +1,50 @@
"ui";
var ColoredButton = (function() {
//继承至ui.Widget
util.extends(ColoredButton, ui.Widget);
function ColoredButton() {
//调用父类构造函数
ui.Widget.call(this);
//自定义属性color定义按钮颜色
this.defineAttr("color", (view, name, defaultGetter) => {
return this._color;
}, (view, name, value, defaultSetter) => {
this._color = value;
view.attr("backgroundTint", value);
});
//自定义属性onClick定义被点击时执行的代码
this.defineAttr("onClick", (view, name, defaultGetter) => {
return this._onClick;
}, (view, name, value, defaultSetter) => {
this._onClick = value;
});
}
ColoredButton.prototype.render = function() {
return (
<button textSize="16sp" style="Widget.AppCompat.Button.Colored" w="auto"/>
);
}
ColoredButton.prototype.onViewCreated = function(view) {
view.on("click", () => {
if (this._onClick) {
eval(this._onClick);
}
});
}
ui.registerWidget("colored-button", ColoredButton);
return ColoredButton;
})();
ui.layout(
<vertical>
<colored-button text="第一个按钮" color="#ff5722"/>
<colored-button text="第二个按钮" onClick="hello()"/>
</vertical>
);
function hello() {
alert("Hello ~");
}

View File

@@ -0,0 +1,54 @@
"ui";
//这个自定义控件是一个勾选框checkbox能够保存自己的勾选状态在脚本重新启动时能恢复状态
var PrefCheckBox = (function() {
//继承至ui.Widget
util.extends(PrefCheckBox, ui.Widget);
function PrefCheckBox() {
//调用父类构造函数
ui.Widget.call(this);
//自定义属性key定义在配置中保存时的key
this.defineAttr("key");
}
PrefCheckBox.prototype.render = function() {
return (
<checkbox />
);
}
PrefCheckBox.prototype.onViewCreated = function(view) {
view.setChecked(PrefCheckBox.getPref().get(this.getKey(), false));
view.on("check", (checked) => {
PrefCheckBox.getPref().put(this.getKey(), checked);
});
}
PrefCheckBox.prototype.getKey = function() {
return this.key || view.attr("id").replace("@+id/", "");
}
PrefCheckBox.setPref = function(pref) {
PrefCheckBox._pref = pref;
}
PrefCheckBox.getPref = function(){
if(!PrefCheckBox._pref){
PrefCheckBox._pref = storages.create("pref_pref");
}
return PrefCheckBox._pref;
}
ui.registerWidget("pref-checkbox", PrefCheckBox);
return PrefCheckBox;
})();
ui.layout(
<vertical>
<pref-checkbox id="perf1" text="配置1"/>
<pref-checkbox id="perf2" text="配置2"/>
<button id="btn" text="获取配置"/>
</vertical>
);
ui.btn.on("click", function(){
toast("配置1为" + PrefCheckBox.getPref().get("perf1"));
toast("配置2为" + PrefCheckBox.getPref().get("perf2"));
});