• This is the sixth day of my participation in the August More Text Challenge.

1. Click to change the text

  • Click the button to change the content of the text

  • You can also replace the text with something like a novel

2. Implementation cases:

  • New project: TextListenerApplication

  • The paragraphs are as follows, with dotted lines separating the different sections

Women are so spoiled! Go out with wife, walk less than 500 meters, she rang tired. I had to get off her back and walk away. -- Women only affect the speed of my drawing the knife, so I throw the knife away, come and make love to me... -- Xiao Ming told fortune when he was a child:26Age yellow robe add body. Sure enough,26Aged into meituan takeout delivery. Calculate of true accurate ~ -- xiao Ming: you say I this poor day lead to when hou be a head? Xiao Hong: That depends on how long you can live.Copy the code

Implementation idea:

  • The texttxtFile copy toprofile

  • To extract file data, in JavaSE generallyI/OThe form of the stream is read out inHarmonyOSIt’s similar, but you don’t use it directlyI/Oflow
  • In the midst of hongmeng, there is a man calledResource managerManagement of theresourcesEverything, as long as it isresourcesEverything in itResource managertube

  • So it can be usedResource managerTo readtxtFile and load the contents of the file in
  • To viewresourcesObject, discovered that he was aI/OFlow, and isI/OIn the flowByte stream

  • So you can read the text file from the stream
  • Select this line of code and hold downCtrl + Alt + TThrown,try-catch

  • Code implementation:

ability_main.xml

  • Give the generated defaultTextText add aidAnd create another onebuttonComponent and addid

      
<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:text1"
        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="40vp"
        />

    <Button
        ohos:id="$+id:but1"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:text="点我"
        ohos:text_size="100"
        ohos:background_element="red"
        >

    </Button>
</DirectionalLayout>
Copy the code

MainAbilitySlice

package com.xdr630.textlistenerapplication.slice;

import com.xdr630.textlistenerapplication.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 {

    Use jokes,text1,but1 in the onClick method, so promote them to member variables
    String[] jokes;
    Text text1;
    Button but1;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        try {
            // Used to concatenate all data read
            StringBuilder sb = new StringBuilder();
            //1. Resource manager
            Resource resource = this.getResourceManager().getResource(ResourceTable.Profile_joke);
            // Since resources is a byte stream, the byte stream can be used to read the contents of the file
            // If the byte stream is used to read Chinese directly, there may be garbled characters, so we need to do a conversion
            // Turn the byte stream into a character stream and read it
            BufferedReader br = new BufferedReader(new InputStreamReader(resource));
            String line;
            while((line = br.readLine()) ! =null){
                sb.append(line);
            }
            // Release resources
            br.close();

            // When the code is executed at this point, all contents of the resource file joke. TXT are read into sb
            // Divide the data into four sections
            //sb is a StringBuffer, and the split method is in String, so convert sb to String and call split
            jokes = sb.toString().split("-");

            // When we click the button, the text box is set with a random joke
            // Find the text component and button component

            text1 = (Text) findComponentById(ResourceTable.Id_text1);

            but1 = (Button) findComponentById(ResourceTable.Id_but1);

            // Add a click event to the button
            but1.setClickedListener(this);
        } catch (IOException e) {
            e.printStackTrace();
        } catch(NotExistException e) { e.printStackTrace(); }}@Override
    public void onActive(a) {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        // When we click the button, we get a random joke from the array and set it to the text
        Random r = new Random();
        // Get random index
        int index = r.nextInt(jokes.length);
        // Get the segment by random index
        String  randomJoke = jokes[index];
        // Place random sections in the texttext1.setText(randomJoke); }}Copy the code
  • After running, the paragraphs are longer than the display

  • So in theability_main.xmlTextAdd an attribute to the text to indicate that if the text is too longWord wrap
ohos:multiple_lines="true"
Copy the code
  • Then run it again: