"ui";
const storage = storageUI('UIConfigInfo')
ui.layout(
<frame >
<vertical margin='20' gravity="center">
<text textSize='25sp' textStyle='bold' gravity='center' textColor='#ff00ff'>本地UI界面数据存储</text>
<vertical>
<input id='input1' w="*" />
<input id='input2' w="*" />
<input id='input3' w="*" />
</vertical>
<button id='btn'>hello world</button>
<radiogroup >
<horizontal gravity="center" w="*">
<radio id='radio1' text='选项1'></radio>
<radio id='radio2' text='选项2'></radio>
<radio id='radio3' text='选项3'></radio>
<radio id='radio4' text='选项4'></radio>
</horizontal>
</radiogroup>
<horizontal gravity="center">
<spinner id="spinner1" entries="男|女|未知" />
<spinner id="spinner2" entries="猪|拱|白菜" />
</horizontal>
<horizontal gravity="center">
<checkbox id='cb1' ></checkbox>
<checkbox id='cb2' ></checkbox>
<checkbox id='cb3' ></checkbox>
<checkbox id='cb4' ></checkbox>
<checkbox id='cb5' ></checkbox>
</horizontal>
<horizontal gravity="center">
<Switch id='switch1' />
<Switch id='switch2' />
<Switch id='switch3' />
</horizontal>
<seekbar id="seekbar" w="*" h="100" max="100" />
</vertical>
</frame>
)
function storageUI(info) {
let HoldUI, ViewGroup, storage = storages.create(info);
events.on('exit', () => { HoldUI(1, ViewGroup) });
ui.post(HoldUI = function (flag, view) {
(view = view || (ViewGroup = activity.getWindow().getDecorView().findViewById(android.R.id.content).getChildAt(0)))
instanceof android.view.ViewGroup && Object.keys(Array.apply(null, new Array(view.childCount)))
.forEach(i => { HoldUI(flag, view.getChildAt(i)); })
if ((id = view.getId().toString()) != -1)
switch (view.getClass().getSimpleName()) {
case 'RadioButton': case 'CheckBox': case 'Switch':
flag ? storage.put(id, view.checked) : view.setChecked(storage.get(id) || view.checked); break;
case 'JsEditText': case 'JsTextView': case 'JsButton':
flag ? storage.put(id, view.getText()) : view.setText(storage.get(id) || view.getText()); break;
case 'SeekBar':
flag ? storage.put(id, view.getProgress()) : view.setProgress(storage.get(id) || view.getProgress()); break;
case 'JsSpinner':
flag ? storage.put(id, view.getSelectedItemPosition()) : view.setSelection(storage.get(id) || view.getSelectedItemPosition()); break;
}
});
return storage;
}
|