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

本次要点:

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下载](https://download.csdn.net/download/qq_44291585/56371334 "HarmonyOS资源管理器的应用.zip-Java文档类资源-CSDN下载")下载源代码

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

相关推荐
captain3763 分钟前
MyBatis(入门5)其他查询操作
java·数据库·spring boot·mybatis
Novproxy_Z29 分钟前
商用代理避坑:如何识别商家拿机房 IP 冒充住宅 IP
java·网络·tcp/ip
文丑颜不良啊1 小时前
LeetCode1481~1500题目解析
java·leetcode
2601_962885721 小时前
quotes 字段全解:涨跌幅、振幅、换手率的口径与单位
java·前端·spring
前端精髓2 小时前
调用openrouter的免费API模型
java·前端·javascript
步行cgn2 小时前
Spring MVC 拦截器 Interceptor 详解:从执行流程到实战应用
java·spring·mvc
文丑颜不良啊2 小时前
LeetCode1381~1400题目解析
java·leetcode
开开心心_Every2 小时前
快速隐藏程序工具自定义快捷键一键切换
java·开发语言·前端·spring·智能手机·word·intellij-idea
萧瑟余晖3 小时前
Spring Batch核心模型与快速上手详解
java·spring·batch
缓慢更新3 小时前
AI 辅助生成单测:从接口定义到用例的流水线
java·服务器·前端·网络·数据库·人工智能