鸿蒙学习笔记之资源管理器(十一)

本次要点:

1.什么是资源管理器

2.资源管理器的应用

1.什么是资源管理器

资源管理器是系统提供的资源管理工具 ,我们可以用它查看本台电脑的所有资源 ,特别是它提供的树形的文件系统结构,使我们能更清楚、更直观地认识电脑的文件和文件夹,这是"我的电脑"所没有的

2.资源管理器的应用

实现一个随机文本的效果,代码如下:

java 复制代码
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 复制代码
<?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下载]下载源代码

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

相关推荐
AD钙奶-lalala1 小时前
Mac OS上搭建 http server
java
伴杯猫5 小时前
【ESP32-IDF】基础外设开发2:系统中断矩阵
c语言·单片机·嵌入式硬件·mcu·物联网·github
爱笑的眼睛115 小时前
HarmonyOS 应用开发新范式:深入探索 Stage 模型与 ArkUI 声明式开发
华为·harmonyos
皮皮林5515 小时前
SpringBoot 全局/局部双模式 Gzip 压缩实战:14MB GeoJSON 秒变 3MB
java·spring boot
小菜全5 小时前
基于若依框架Vue+TS导出PDF文件的方法
javascript·vue.js·前端框架·json
weixin_456904275 小时前
Spring Boot 用户管理系统
java·spring boot·后端
趁你还年轻_5 小时前
异步编程CompletionService
java
DKPT6 小时前
Java内存区域与内存溢出
java·开发语言·jvm·笔记·学习
资深前端之路6 小时前
react 面试题 react 有什么特点?
前端·react.js·面试·前端框架
sibylyue6 小时前
Guava中常用的工具类
java·guava