本次要點:
1.什么是資源管理器
2.資源管理器的應用
1.什么是資源管理器
資源管理器是系統提供的資源管理工具,我們可以用它查看本臺電腦的所有資源,特別是它提供的樹形的檔案系統結構,使我們能更清楚、更直觀地認識電腦的檔案和檔案夾,這是“我的電腦”所沒有的
2.資源管理器的應用
實作一個隨機文本的效果,代碼如下:
package com.example.mydeomresourcemanager.slice;
import com.example.mydeomresourcemanager.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.Text;
import ohos.global.resource.NotExistException;
import ohos.global.resource.Resource;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
Button btn;
Text txt;
String[] joker;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
try {
//用來保存拼接的讀取到的所有資料
StringBuilder sb = new StringBuilder();
//資源管理器
Resource resource = this.getResourceManager().getResource(ResourceTable.Profile_profile);
//因為resource是位元組流利用位元組流可以讀取檔案中的內容
BufferedReader br = new BufferedReader(new InputStreamReader(resource));
String line;
while ((line=br.readLine())!=null)
{
sb.append(line);
}
//釋放資源
br.close();
joker = sb.toString().split("---");
//獲取text標簽展示資料,button標簽隨機獲取一個資料
btn = (Button) findComponentById(ResourceTable.Id_btn1);
txt = (Text) findComponentById(ResourceTable.Id_txt1);
//給btn組件系結一個點擊事件
btn.setClickedListener(this);
} catch (IOException e) {
e.printStackTrace();
} catch (NotExistException e) {
e.printStackTrace();
}
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
@Override
public void onClick(Component component) {
//隨機生成一個保存文本內容的長度亂數
Random r=new Random();
int i = r.nextInt(joker.length);
//保存一個文本內容
String str=joker[i];
//設定文本組件的內容
txt.setText(str);
}
}
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">
<Text
ohos:id="$+id:txt1"
ohos:height="match_content"
ohos:width="match_content"
ohos:background_element="$graphic:background_ability_main"
ohos:layout_alignment="horizontal_center"
ohos:text="$string:mainability_HelloWorld"
ohos:text_size="24vp"
ohos:multiple_lines="true"
/>
<Button
ohos:height="match_content"
ohos:width="match_content"
ohos:text="點擊讀取檔案內容"
ohos:id="$+id:btn1"
ohos:text_size="30vp"
ohos:text_color="#FF0000FF"
/>
</DirectionalLayout>
初始化頁面:
第一次點擊:
第二次點擊:
每次點擊都會隨機生成一個文本內容展示出來,
知識點:
1.new StringBuilder():是一個可變的字符序列, 此類提供一個與StringBuffer 兼容的API,但不保證同步, 該類被設計用作StringBuffer 的一個簡易替換,用在字串緩沖區被單個執行緒使用的時候(這種情況很普遍)
2.getResourceManager().getResource:資源管理器,獲取resources資源下的檔案
3.new BufferedReader():緩沖字符輸入流,它繼承于Reader, BufferedReader的作用是為其他字符輸入流添加一些緩沖功能, 創建BufferedReader時,我們會通過它的建構式指定某個Reader為引數
4.new Random():這里宣告了一個物件rand,后面就用rand來構造亂數的范圍和型別了
5.nextInt():隨機產生某個范圍內的整數
以上就是本次所應用到的知識點,點擊[HarmonyOS資源管理器的應用.zip-Java檔案類資源-CSDN下載]下載源代碼
分享不易,都觀看到這里了,還不點贊收藏嘛!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/375017.html
標籤:其他
