UI界面数据存储_Autojs

6 篇文章 0 订阅
"ui";
const storage = storageUI('UIConfigInfo')
// storage.clear()
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>
)

/**
 * Autojs本地UI界面数据存储
 * @param {本地存储名称} info 
 */
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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值