一、事件
事件就是可以被识别的操作 。
常见的事件有:单击、双击、长按、还有触摸事件 。
1.单击事件(常用)
接口名:ClickedListener
又叫:点击事件。
2.事件的四种写法:
(1)自己编写实现类
public class MainAbilitySlice extends AbilitySlice {
Text text1 = null;
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
//1.找到按钮
Button but1 = (Button) findComponentById(ResourceTable.Id_but1);
text1 = (Text) findComponentById(ResourceTable.Id_text1);
//2.给按钮绑定一个单击事件
but1.setClickedListener(new MyListener());
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
public void onClick(Component component) {
Button btu = (Button) component;
btu.setText("被点了-单击事件的第二种写法");
text1.setText("被点击了");
}
}
class MyListener implements Component.ClickedListener{
@Override
public void onClick(Component component) {
//component:所有组件的父类
//参数:被点击的组件对象
//component.setText();
Button btu = (Button) component;
btu.setText("被点了");
}
}
(2).当前类实现接口
public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
Text text1 = null;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
//1.找到按钮
Button but1 = (Button) findComponentById(ResourceTable.Id_but1);
text1 = (Text) findComponentById(ResourceTable.Id_text1);
//2.给按钮绑定一个单击事件
but1.setClickedListener(this);
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
public void onClick(Component component) {
Button btu = (Button) component;
btu.setText("被点了-单击事件的第二种写法");
text1.setText("被点击了");
}
}
(3)匿名内部
public class MainAbilitySlice extends AbilitySlice {
Text text1 = null;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
//1.找到按钮
Button but1 = (Button) findComponentById(ResourceTable.Id_but1);
text1 = (Text) findComponentById(ResourceTable.Id_text1);
//2.给按钮绑定一个单击事件
but1.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
Button btu = (Button) component;
btu.setText("被点了-单击事件的第三种写法");
text1.setText("被点击了");
}
});
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
(4)匿名方法类
public class MainAbilitySlice extends AbilitySlice {
Text text1 = null;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
//1.找到按钮
Button but1 = (Button) findComponentById(ResourceTable.Id_but1);
text1 = (Text) findComponentById(ResourceTable.Id_text1);
//2.给按钮绑定一个单击事件
but1.setClickedListener(this::onClick);
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
public void onClick(Component component) {
Button btu = (Button) component;
btu.setText("被点了-单击事件的第二种写法");
text1.setText("被点击了");
}
}
2.双击事件
3.长按事件
4.滑动事件
也叫做触摸事件。
接口名:TouchEventListener
包含以下两部分知识点:
滑动事件里面分为三个动作:按下不松,移动,抬起。
PRIMARY_POINT_DOWN:按下不松。
POINT_MOVE:移动。
PRIMARY_POINT_UP:抬起。
手机坐标:
手机左上角的点为原点。
向右为X轴
向下为Y轴
垂直于屏幕向上为Z轴
方法返回值:
true表示继续执行后面的动作。
false表示不会继续执行后面的动作。
5.多按钮被点击
6.双击点赞双击取消
7.单机出现随机文本
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);
}
}
8.单机出现随即图片
9.统计点击次数