第二种和第四种常用
实现步骤:
1.通过id找到组件。
2.给需要的组件设置单击事件。
3.实现ClickedListener接口。
4.重写onClicked方法。
第一种,自定义实现类(在当前类外面写实现类)
①MainAbilitySlice.java文件
package com.example.yeman.slice;import com.example.yeman.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;public class MainAbilitySlice extends AbilitySlice {@Overridepublic void onStart(Intent intent) {super.onStart(intent);super.setUIContent(ResourceTable.Layout_ability_main);//找到按钮。// 说明:findComponentById返回的是父类对象所有组件,因此需要(Button)强转。Button but = (Button) findComponentById(ResourceTable.Id_but);//给按钮绑定一个点击事件but.setClickedListener(new MyListenner());}@Overridepublic void onActive() {super.onActive();}@Overridepublic void onForeground(Intent intent) {super.onForeground(intent);}
}//第一种,自定义实现类
class MyListenner implements Component.ClickedListener{@Overridepublic void onClick(Component component) {//component所有组件的父类//参数是被点击的组件Button btn = (Button)component;btn.setText("第1种被点");}
}
②ability_main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="match_parent"ohos:width="match_parent"ohos:alignment="center"ohos:orientation="vertical"><Buttonohos:id="$+id:but"ohos:height="match_content"ohos:width="match_content"ohos:background_element="blue"ohos:text="请点击我"ohos:text_size="150"/></DirectionalLayout>
第二种,当前类作为实现类(在当前类引入)
比第一种好在可以改变其他组件,如代码中,点击按钮,修改文本组件文本内容。
①MainAbilitySlice.java文件
package com.example.yeman.slice;import com.example.yeman.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 txt;@Overridepublic void onStart(Intent intent) {super.onStart(intent);super.setUIContent(ResourceTable.Layout_ability_main);//找到按钮。// 说明:findComponentById返回的是父类对象所有组件,因此需要(Button)强转。Button but = (Button) findComponentById(ResourceTable.Id_but);//找到文本框组件txt = (Text) findComponentById(ResourceTable.Id_txt);//给按钮绑定一个点击事件but.setClickedListener(this);}@Overridepublic void onActive() {super.onActive();}@Overridepublic void onForeground(Intent intent) {super.onForeground(intent);}//第二种,当前实现类@Overridepublic void onClick(Component component) {//component所有组件的父类//参数是被点击的组件txt.setText("你点击了按钮");}
}
②ability_main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="match_parent"ohos:width="match_parent"ohos:alignment="center"ohos:orientation="vertical"><Textohos:id="$+id:txt"ohos:height="match_content"ohos:width="match_content"ohos:text="这是文本组件"ohos:text_size="100"/><Buttonohos:id="$+id:but"ohos:height="match_content"ohos:width="match_content"ohos:background_element="blue"ohos:text="请点击我"ohos:text_size="150"/></DirectionalLayout>
第三种,匿名内部类(只能用一次)
①MainAbilitySlice.java文件
package com.example.yeman.slice;import com.example.yeman.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 {Text txt;@Overridepublic void onStart(Intent intent) {super.onStart(intent);super.setUIContent(ResourceTable.Layout_ability_main);//找到按钮。// 说明:findComponentById返回的是父类对象所有组件,因此需要(Button)强转。Button but = (Button) findComponentById(ResourceTable.Id_but);//找到文本框组件txt = (Text) findComponentById(ResourceTable.Id_txt);//给按钮绑定一个点击事件but.setClickedListener(new Component.ClickedListener() { //第三种,匿名内部类@Overridepublic void onClick(Component component) {//component所有组件的父类//参数是被点击的组件txt.setText("你点击了按钮");}});}@Overridepublic void onActive() {super.onActive();}@Overridepublic void onForeground(Intent intent) {super.onForeground(intent);}}
②ability_main.xml文件同第二种
第四种,引用方法
①MainAbilitySlice.java文件
package com.example.yeman.slice;import com.example.yeman.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 {Text txt;@Overridepublic void onStart(Intent intent) {super.onStart(intent);super.setUIContent(ResourceTable.Layout_ability_main);//找到按钮。// 说明:findComponentById返回的是父类对象所有组件,因此需要(Button)强转。Button but = (Button) findComponentById(ResourceTable.Id_but);//找到文本框组件txt = (Text) findComponentById(ResourceTable.Id_txt);//给按钮绑定一个点击事件but.setClickedListener(this::onClick);}@Overridepublic void onActive() {super.onActive();}@Overridepublic void onForeground(Intent intent) {super.onForeground(intent);}//第四种,引用方法public void onClick(Component component) {//component所有组件的父类//参数是被点击的组件txt.setText("你点击了按钮");}
}
②ability_main.xml文件同第二种