- 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 text
txt
File copy toprofile
- To extract file data, in JavaSE generally
I/O
The form of the stream is read out inHarmonyOS
It’s similar, but you don’t use it directlyI/O
flow - In the midst of hongmeng, there is a man calledResource managerManagement of the
resources
Everything, as long as it isresources
Everything in itResource managertube
- So it can be usedResource managerTo read
txt
File and load the contents of the file in - To view
resources
Object, discovered that he was aI/O
Flow, and isI/O
In the flowByte stream
- So you can read the text file from the stream
- Select this line of code and hold down
Ctrl + Alt + T
Thrown,try-catch
- Code implementation:
ability_main.xml
- Give the generated default
Text
Text add aid
And create another onebutton
Component 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 the
ability_main.xml
的Text
Add 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: