本文主要是介绍08. 案例3 随机更换段子,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
实现效果
点击按钮实现文本切换
joke.txt 代码:https://download.csdn.net/download/u010171285/20660514
说明:
将 joke.txt 放入 profile文件夹中,不要随便更改文本存放路径。
代码实例:
ability_main.xml
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayoutohos:id="$+id:dl"xmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="match_parent"ohos:width="match_parent"ohos:alignment="center"ohos:orientation="vertical"><Textohos:id="$+id:text1"ohos:height="match_content"ohos:width="match_content"ohos:background_element="$graphic:background_ability_main"ohos:layout_alignment="horizontal_center"ohos:text= "Hello World"ohos:text_size="40vp"ohos:multiple_lines="true"/><Buttonohos:id="$+id:but1"ohos:height="match_content"ohos:width="match_content"ohos:text="点我"ohos:text_size="100"ohos:background_element="red"/></DirectionalLayout>
MainAbilitySlice.java
package com.example.myapplication.slice;import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.OnClickListener;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;
import ohos.global.resource.NotExistException;
import ohos.global.resource.Resource;
import ohos.javax.xml.stream.events.EndElement;
import ohos.multimodalinput.event.MmiPoint;
import ohos.multimodalinput.event.TouchEvent;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;import static com.example.myapplication.ResourceTable.Media_red;
import static com.example.myapplication.ResourceTable.Media_white;public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {String[] jokes;Text text1;Button but1;@Overridepublic void onStart(Intent intent) {super.onStart(intent);super.setUIContent(ResourceTable.Layout_ability_main);/*1. 资源管理器:管理视频,图片,文本等,只要是resources文件夹里的文件都归资源管理器管 */try {//用来拼接读取到的所有数据StringBuilder sb = new StringBuilder();Resource resource = this.getResourceManager().getResource(ResourceTable.Profile_joke);//获得资源管理器
//因为resources是一个字节流利用字节流可以读取文件中的内容//如果用字节流读取中文,可能会出现乱码BufferedReader br = new BufferedReader(new InputStreamReader(resource));String line;while ((line = br.readLine()) != null){sb.append(line);}//释放资源br.close();//当代码执行到这里的时候,资源文件joke.txt中所有的内容全部读取到sb当中了//利用---将数据进行切割,分成四个段子jokes = sb.toString().split("---");//当点击了按钮之后就会给文本框设置一个随机笑话//找到文本组件和按钮组件text1 = (Text) findComponentById(ResourceTable.Id_text1);but1 = (Button) findComponentById(ResourceTable.Id_but1);//给按钮添加一个单击事件but1.setClickedListener(this);} catch (IOException e) {e.printStackTrace();} catch (NotExistException e) {e.printStackTrace();}}@Overridepublic void onActive() {super.onActive();}@Overridepublic void onClick(Component component) {//当我们点击按钮之后,从数组里面随机获取一个笑话并获取到文本当中Random r = new Random();//获取随机索引int index = r.nextInt(jokes.length);//通过随机索引来获取段子String randomJoke = jokes[index];//把随机的段子设置到文本当中text1.setText(randomJoke);}
}
这篇关于08. 案例3 随机更换段子的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!