UI界面数据存储_Autojs

**/**
 * Autojs本地UI界面数据存储
 * @param {本地存储名称} info 
 * 2022-4-4修改:新增checked="off"控制存在id的view是否通过存储保存数据
 * 2022-4-7修改:控件选择上用===nul判断是否需要自动存储,对于checked不为空的对象,不在自动存储,解决悬浮窗无障碍开关问题,修改开关类setChecked方式解决当选项为false是使用页面状态(虽然===null之后并不会有影响)
 * 2022-7-11修改:优化整体逻辑,不存储list和grid信息,解决数据闪现问题,存储以正常id名称实时存储
 * 感谢惜缘的大力帮助
 */**
"ui";

ui.layout(
  <frame >
    <vertical margin='20' gravity="center">
      <text textSize='25sp' textStyle='bold' gravity='center' textColor='#ff00ff'>本地UI界面数据存储</text>
      <vertical>
        <input id='input1' w="*" text="5445" />
        <input id='input2' w="*" />
        <input id='input3' w="*" />
      </vertical>
      <radiogroup orientation="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>
      </radiogroup>
      <horizontal gravity="center">
        <spinner id="spinner1" entries="男|女|未知" />
        <spinner id="spinner2" entries="猪|拱|白菜" />
      </horizontal>
      <horizontal gravity="center">
        <checkbox id='cb1' checked="off" ></checkbox> {/* 不保存数据 */}
        <checkbox id='cb2' ></checkbox>
        <checkbox id='cb3' ></checkbox>
        <checkbox id='cb4' ></checkbox>
        <checkbox id='cb5' ></checkbox>
      </horizontal>
      <horizontal gravity="center">
        <Switch id='switch1' checked="{{auto.service != null}}" /> {/* 不保存数据,动态获取无障碍状态 */}
        <Switch id='switch2' />
        <Switch id='switch3' />
      </horizontal>
      <seekbar id="seekbar" w="*" h="100" max="100" />
      <grid id="grid" spanCount="3" margin="5">
        <horizontal w="*" h="40" margin="5" gravity="center_vertical" elevation="1" bg='#fafafa' foreground="?selectableItemBackground">
          <checkbox id="select" checked="{{this.select}}" marginLeft="2dp" />
          <text id="title" text="{{this.title}}" textColor="#000000" textSize="18dp" paddingBottom="2" maxLines="1" ellipsize="end" layout_weight="1" />
          <img id='delete' w='20' h='20' marginRight="5" background='@drawable/ic_close_white_24dp' backgroundTint='#FF0000' clickable='true' />
        </horizontal>
      </grid>
    </vertical>
  </frame>
)

const storage = storageUI('UIConfigInfo11')
// log(JSON.stringify(storage._storage))//查看本地存储信息
// log(files.read("/data/user/0/org.autojs.autojspro/shared_prefs/autojs.localstorage.UIConfigInfo11.xml"))
// storage.clear()
function storageUI(info) {
  let HoldUI, ViewGroup, storage = storages.create(info);
  // events.on('exit', () => { HoldUI(1, ViewGroup) });
  let mTime = new Date().getTime();
  ui.view.getViewTreeObserver().addOnDrawListener({
    onDraw: function () {
      if (new Date().getTime() - mTime >= 500) {
        mTime = new Date().getTime();
        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 => {
          /(JsGridView|JsListView)/.test(view.getClass().getSimpleName()) ||
            HoldUI(flag, view.getChildAt(i));
        })
    if ((id = view.attr('id')) != null && (id = id.replace('@+id/', ''))) {
      moff = view.attr("checked") !== "off"
      mundefined = ((typeof storage.get(id)) != "undefined")
      switch (view.getClass().getSimpleName()) {
        case 'RadioButton': case 'CheckBox': case 'Switch':/* 单选框|复选框|开关 */
          moff && flag ? storage.put(id, view.checked) : mundefined && view.setChecked(Boolean(storage.get(id))); break;
        case 'JsEditText':/* 输入框 */
          moff && flag ? storage.put(id, view.getText()) : mundefined && view.setText(storage.get(id)); break;
        case 'SeekBar':/* 进度条 */
          moff && flag ? storage.put(id, view.getProgress()) : mundefined && view.setProgress(Number(storage.get(id))); break;
        case 'JsSpinner':/* 下拉框 */
          moff && flag ? storage.put(id, view.getSelectedItemPosition()) : mundefined && view.setSelection(Number(storage.get(id))); break;
      }
    }
  });
  return storage;
}



var items = storage.get("items", ["梅州", "河源", "揭阳", "深圳", "惠州"].map(item => { return { select: false, title: item } }));
ui.grid.setDataSource(items);

var mTime = new Date().getTime();
ui.view.getViewTreeObserver().addOnDrawListener({
  onDraw: function () {
    if (new Date().getTime() - mTime >= 500) {
      mTime = new Date().getTime();
      storage.put("items", items);
    }
  }
});

ui.grid.on("item_bind", function (itemView, itemHolder) {
  //绑定勾选框事件
  itemView.select.on("check", function (checked) {
    let item = itemHolder.item;
    item.select = checked;
  });
  //删除数据 
  itemView.delete.on('click', () => {
    let item = itemHolder.item;
    confirm("确定要删除" + item.title + "吗?").then(ok => {
      if (ok) {
        items.splice(itemHolder.position, 1);
      };
    });
  });
});

ui.grid.on("item_click", function (item, i, itemView, listView) {
  itemView.select.checked = !itemView.select.checked;
});

以下为原来数据

/**2022-4-4修改:新增checked="off"控制存在id的view是否通过存储保存数据*/
/**2022-4-7修改:控件选择上用===nul判断是否需要自动存储,对于checked不为空的对象,不在自动存储,解决悬浮窗无障碍开关问题,修改开关类setChecked方式解决当选项为false是使用页面状态(虽然===null之后并不会有影响) */
"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' checked="off" ></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':/* 单选框|复选框|开关 */
          view.attr("checked") === null && flag ? storage.put(id, view.checked) : view.setChecked(storage.get(id) === undefined ? view.checked : Boolean(storage.get(id))); break;
        case 'JsEditText': case 'JsTextView': case 'JsButton':/* 输入框|文本框|按钮 */
          view.attr("checked") === null && flag ? storage.put(id, view.getText()) : view.setText(storage.get(id) || view.getText()); break;
        case 'SeekBar':/* 进度条 */
          view.attr("checked") === null && flag ? storage.put(id, view.getProgress()) : view.setProgress(Number(storage.get(id)) || view.getProgress()); break;
        case 'JsSpinner':/* 下拉框 */
          view.attr("checked") === null && flag ? storage.put(id, view.getSelectedItemPosition()) : view.setSelection(Number(storage.get(id)) || view.getSelectedItemPosition()); break;
      }
  });
  return storage;
}

你可能感兴趣的:(js,Autojs,ui,android)