赞
踩
package com.example.listenerapplication.slice;
import com.example.listenerapplication.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 {
String[] jokes;
Text text1;
Button btu1;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
try {
//用来拼接读取到的所有数据
StringBuilder sb = new StringBuilder();
//1.资源管理器
Resource resource = this.getResourceManager().getResource(ResourceTable.Profile_joke);
//因为resource是一个字节流,利用字节流可以读取文件中的内容
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);
btu1 = (Button) findComponentById(ResourceTable.Id_btu1);
//给按钮添加一个单击事件
btu1.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 index = r.nextInt(jokes.length);
//通过随机索引获取段子
String randomJoke = jokes[index];
//把随机的段子设置到文本当中
text1.setText(randomJoke);
}
}
package com.example.listenerapplication.slice;
import com.example.listenerapplication.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.Image;
import java.util.ArrayList;
import java.util.Random;
public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
ArrayList<Integer> list = new ArrayList<>();
Image image;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
//定义一个数组或者集合用来存储所有的图片
list.add(ResourceTable.Media_girl1);
list.add(ResourceTable.Media_girl2);
list.add(ResourceTable.Media_girl3);
list.add(ResourceTable.Media_girl4);
list.add(ResourceTable.Media_girl5);
list.add(ResourceTable.Media_girl6);
list.add(ResourceTable.Media_girl7);
list.add(ResourceTable.Media_girl8);
list.add(ResourceTable.Media_girl9);
//找到组件
image = (Image) findComponentById(ResourceTable.Id_img);
Button btu1 = (Button) findComponentById(ResourceTable.Id_but1);
//给按钮绑定单击事件
btu1.setClickedListener(this);
}
@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 index = r.nextInt(list.size());
//通过随机的索引,可以获取随机的元素
int randomImg = list.get(index);
//把获取到的随机图片设置给Image组件就可以了
image.setImageAndDecodeBounds(randomImg);
}
}
package com.example.listenerapplication.slice;
import com.example.listenerapplication.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;
public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
Text text;
Button btu;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
//找到文本和按钮对象
text = (Text) findComponentById(ResourceTable.Id_text1);
btu = (Button) findComponentById(ResourceTable.Id_btu1);
//给按钮设置单击事件
btu.setClickedListener(this);
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
//如果flag为true表示当前按钮是第一次点击
//如果flag为false表示当前按钮不是第一次点击
boolean flag = true;
long startTime = 0;
//用来记录点击了多少次
int count = 0;
@Override
public void onClick(Component component) {
//点一次,计数器就自增一次。
count++;
//统计10秒之内,按了多少次
//并把次数在文本框展示出来
if(flag){
//如果当前是第一次点击按钮
//记录当前的时间
startTime = System.currentTimeMillis();
//当第一次点击之后,游戏开始
//修改按钮中的文字内容
btu.setText("请疯狂点我");
//修改标记
flag = false;
}else{
if((System.currentTimeMillis() - startTime) <= 10000 ){
text.setText(count + "");
}else{
btu.setText("结束");
//取消按钮的点击事件,让该按钮不能再被点击了
btu.setClickable(false);
}
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。