需求:有两台设备,在左边的设备中上下两个文本框中输入数据,点击保存,右边的设备通过key获取数据且获取到的数据和左边的设备是一致的
ability_main.xml
<?xml version="1.0" encoding="utf-8"?> <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:height="match_parent" ohos:width="match_parent" ohos:alignment="center" ohos:orientation="vertical"> <TextField ohos:id="$+id:key" ohos:hint="key" ohos:text_size="20fp" ohos:height="match_content" ohos:width="match_parent" ohos:left_margin="20vp"></TextField> <TextField ohos:id="$+id:value" ohos:hint="value" ohos:text_size="20fp" ohos:bottom_margin="20vp" ohos:top_margin="20vp" ohos:left_margin="20vp" ohos:height="match_content" ohos:width="match_parent"></TextField> <Button ohos:id="$+id:saveData" ohos:background_element="$graphic:background_ability_main" ohos:height="match_content" ohos:width="match_parent" ohos:left_margin="20fp" ohos:text_size="20vp" ohos:right_margin="20vp" ohos:text="保存数据"> </Button> <Button ohos:id="$+id:deleteData" ohos:background_element="$graphic:background_ability_main" ohos:height="match_content" ohos:width="match_parent" ohos:top_margin="10vp" ohos:left_margin="20fp" ohos:text_size="20vp" ohos:right_margin="20vp" ohos:text="删除"> </Button> <Button ohos:id="$+id:getData" ohos:background_element="$graphic:background_ability_main" ohos:height="match_content" ohos:width="match_parent" ohos:left_margin="20fp" ohos:text_size="20vp" ohos:top_margin="10vp" ohos:right_margin="20vp" ohos:text="获取数据"> </Button> <TextField ohos:text_alignment="center" ohos:background_element="#cccccc" ohos:hint="展示数据" ohos:top_margin="20vp" ohos:id="$+id:getContent" ohos:text_size="20fp" ohos:height="100vp" ohos:width="match_parent" ohos:left_margin="20vp" ohos:right_margin="20vp" ></TextField> </DirectionalLayout>
MainAbilitySlice.java
package com.gulixiong.distributeddata.slice; import com.gulixiong.distributeddata.ResourceTable; import ohos.aafwk.ability.AbilitySlice; import ohos.aafwk.content.Intent; import ohos.agp.components.Button; import ohos.agp.components.Component; import ohos.agp.components.TextField; import ohos.data.distributed.common.*; import ohos.data.distributed.user.SingleKvStore; public class MainAbilitySlice extends AbilitySlice { private TextField key; private TextField value; private Button saveData; private Button getData; private TextField getContent; private SingleKvStore kvStore; @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); initDB(); initUI(); setListeners(); } private void initDB() { //创建分布式数据库管理实例 KvManagerConfig config=new KvManagerConfig(this); KvManager kvManager = KvManagerFactory.getInstance().createKvManager(config); //创建分布式数据库 Options options=new Options(); options.setCreateIfMissing(true).setEncrypt(false).setKvStoreType(KvStoreType.SINGLE_VERSION); //指定数据库名称 String dbName="testDb"; kvStore=kvManager.getKvStore(options,dbName); } private void initUI() { key = (TextField) findComponentById(ResourceTable.Id_key); value = (TextField) findComponentById(ResourceTable.Id_value); saveData= (Button) findComponentById(ResourceTable.Id_saveData); getData= (Button) findComponentById(ResourceTable.Id_getData); getContent = (TextField) findComponentById(ResourceTable.Id_getContent); } private void setListeners(){ saveData.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { saveData(); } }); getData.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { getData(); } }); } /** * 保存数据 */ private void saveData(){ String keyData=key.getText(); String valueData=value.getText(); kvStore.putString(keyData,valueData); getContent.setText("保存数据成功"); } private void getData(){ String keyData=key.getText(); String content = kvStore.getString(keyData); getContent.setText(content); } private void deleteData(){ String keyData=key.getText(); kvStore.delete(keyData); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } }
注意:两个设备要同时出现相同的界面的操作如下:
点击第一个箭头处的三角,会看到有两台设备,默认上面的设备框中展示的是编号小的设备,点击运行按钮
将设备列表切换到第二台设备,然后点击运行按钮
双击第一台设备上你写的程序
效果如下:
现在就可以测试了