鸿蒙学习笔记之资源管理器

本次要点:

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下载]下载源代码

分享不易,都观看到这里了,还不点赞收藏嘛!

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码

)">
< <上一篇
下一篇>>