当前位置:   article > 正文

HarmonyOS 读取文件 随机更换段子_harmonyos fs 文件读取

harmonyos fs 文件读取

base/profile下存放一个joke.txt文件

里面又三个笑话并且用---分割

  1. “大爷,我现场采访您一下,您这样晨跑锻炼坚持几年了?”
  2. “姑娘别挡道!我尿急! ”
  3. “请问你是做什么工作的?”
  4. ---
  5. “哦。我的工作是杀僵尸。”
  6. “嗯?可是这个世界上没有僵尸啊! ”
  7. “你以为它们是怎么没有的?”
  8. ---
  9. 中午去买菜,感觉都不太新鲜了。
  10. 老板:早上刚到的,都新鲜的。
  11. 我:这菜看着就蔫蔫的啊?!
  12. 老板:从早上到现在,它以为没人要自己了,这不垂头丧气么!
  13. 我。。。

布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <DirectionalLayout
  3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4. ohos:height="match_parent"
  5. ohos:width="match_parent"
  6. ohos:alignment="center"
  7. ohos:orientation="vertical">
  8. <Text
  9. ohos:id="$+id:joke"
  10. ohos:height="match_content"
  11. ohos:width="match_content"
  12. ohos:background_element="$graphic:background_ability_joke"
  13. ohos:layout_alignment="horizontal_center"
  14. ohos:multiple_lines="true"
  15. ohos:text="$string:jokeability_HelloWorld"
  16. ohos:text_size="40vp"
  17. />
  18. <Button
  19. ohos:id="$+id:btnReadRandom"
  20. ohos:height="match_content"
  21. ohos:width="match_content"
  22. ohos:background_element="$graphic:background_ability_joke"
  23. ohos:layout_alignment="horizontal_center"
  24. ohos:text="读取随机笑话"
  25. ohos:text_size="40vp"
  26. />
  27. </DirectionalLayout>

     ohos:multiple_lines="true" 允许换行

代码

  1. package com.example.helloworld.slice;
  2. import com.example.helloworld.ResourceTable;
  3. import ohos.aafwk.ability.AbilitySlice;
  4. import ohos.aafwk.content.Intent;
  5. import ohos.agp.components.Button;
  6. import ohos.agp.components.Component;
  7. import ohos.agp.components.Text;
  8. import ohos.global.resource.NotExistException;
  9. import ohos.global.resource.Resource;
  10. import java.io.BufferedReader;
  11. import java.io.IOException;
  12. import java.io.InputStreamReader;
  13. import java.util.Random;
  14. public class JokeAbilitySlice extends AbilitySlice {
  15. @Override
  16. public void onStart(Intent intent) {
  17. super.onStart(intent);
  18. super.setUIContent(ResourceTable.Layout_ability_joke);
  19. Text text = findComponentById(ResourceTable.Id_joke);
  20. Button btnReadRandom = findComponentById(ResourceTable.Id_btnReadRandom);
  21. StringBuilder sb = new StringBuilder();
  22. try {
  23. //返回文件的字节流
  24. Resource resource = this.getResourceManager().getResource(ResourceTable.Profile_joke);
  25. //字节流
  26. BufferedReader br = new BufferedReader(new InputStreamReader(resource));
  27. String line;
  28. while ((line = br.readLine()) != null) {
  29. sb.append(line);
  30. }
  31. br.close();
  32. } catch (IOException e) {
  33. e.printStackTrace();
  34. } catch (NotExistException e) {
  35. e.printStackTrace();
  36. }
  37. btnReadRandom.setClickedListener(new Component.ClickedListener() {
  38. @Override
  39. public void onClick(Component component) {
  40. String[] jocks = sb.toString().split("---");
  41. Random random = new Random();
  42. int index = random.nextInt(jocks.length);
  43. text.setText(jocks[index]);
  44. }
  45. });
  46. }
  47. @Override
  48. public void onActive() {
  49. super.onActive();
  50. }
  51. @Override
  52. public void onForeground(Intent intent) {
  53. super.onForeground(intent);
  54. }
  55. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/277216
推荐阅读
相关标签
  

闽ICP备14008679号