sample: 自定义控件; fix: ActivityNotFoundException
This commit is contained in:
50
app/src/main/assets/sample/界面控件/自定义控件-带颜色按钮.js
Normal file
50
app/src/main/assets/sample/界面控件/自定义控件-带颜色按钮.js
Normal 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 ~");
|
||||
|
||||
}
|
||||
54
app/src/main/assets/sample/界面控件/自定义控件-配置勾选框.js
Normal file
54
app/src/main/assets/sample/界面控件/自定义控件-配置勾选框.js
Normal 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"));
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user