网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
super.setUIContent(ResourceTable.Layout_ability_main);
//找到按钮。
// 说明:findComponentById返回的是父类对象所有组件,因此需要(Button)强转。
Button but = (Button) findComponentById(ResourceTable.Id_but);
//给按钮绑定一个点击事件
but.setClickedListener(new MyListenner());
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
//第一种,自定义实现类
class MyListenner implements Component.ClickedListener{
@Override
public void onClick(Component component) {
//component所有组件的父类
//参数是被点击的组件
Button btn = (Button)component;
btn.setText(“第1种被点”);
}
}
②ability_main.xml文件
<?xml version="1.0" encoding="utf-8"?><DirectionalLayout
xmlns:ohos=“http://schemas.huawei.com/res/ohos”
ohos:height=“match_parent”
ohos:width=“match_parent”
ohos:alignment=“center”
ohos:orientation=“vertical”>
<Button
ohos:id=“$+id:but”
ohos:height=“match_content”
ohos:width=“match_content”
ohos:background_element=“blue”
ohos:text=“请点击我”
ohos:text_size=“150”/>
第二种,当前类作为实现类(在当前类引入)
比第一种好在可以改变其他组件,如代码中,点击按钮,修改文本组件文本内容。
①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;
@Override
public 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);
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
//第二种,当前实现类
@Override
public void onClick(Component component) {
//component所有组件的父类
//参数是被点击的组件
txt.setText(“你点击了按钮”);
}
}
②ability_main.xml文件
<?xml version="1.0" encoding="utf-8"?><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:txt”
ohos:height=“match_content”
ohos:width=“match_content”
ohos:text=“这是文本组件”
ohos:text_size=“100”/>
<Button
ohos:id=“$+id:but”
ohos:height=“match_content”
ohos:width=“match_content”
ohos:background_element=“blue”
ohos:text=“请点击我”
ohos:text_size=“150”/>
第三种,匿名内部类(只能用一次)
①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;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
//找到按钮。
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
;
//找到按钮。
[外链图片转存中…(img-52VvCuKy-1715626262480)]
[外链图片转存中…(img-1kzr2pZq-1715626262481)]
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!