DevEcoStudio的组件布局和事件内容(笔记)本⽂选择的是Java开发模式,不是Js+Java的开发模式
⼀、组件、布局、事件
组件:屏幕展⽰出来的元素,⽂本、按钮、图⽚等。
布局:组件摆放⽅式
事件:可以被组件识别的操作
⼆、事件
常见的有:单击、双击、长按、滑动等
1.单击事件
第⼀种写法
⾃⼰定义实现类
修改l
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="schemas.huawei/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:alignment="center"
ohos:orientation="vertical">
<Button
ohos:id="$+id:but1"
ohos:height="match_content"
ohos:width="match_content"
ohos:text="click me"
ohos:text_size="100"
ohos:background_element="red"
/>
</DirectionalLayout>
为对应的页⾯写上如下代码
application.slice;
application.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import t.Intent;
import ohos.agpponents.Button;
import ohos.agpponents.Component;
public class MainAbilitySlice extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.setUIContent(ResourceTable.Layout_ability_main);
Button but1 = (Button)findComponentById(ResourceTable.Id_but1);
but1.setClickedListener((new MyListener()));
}
@Override
public void onActive() {
}
@Override
public void onForeground(Intent intent) {
}
}
class MyListener implements Component.ClickedListener{
@Override
//传⼊的参数指的是被点击的控件
public void onClick(Component component){
Button but = (Button)component;
but.setText("Clicked");
}
}
第⼆种写法
当前类作为实现类,即上⼀篇⽂章中的⽤法
不过这⾥将增添⼀个⽂本,让其随点击事件⽽变化
由此可以知,第⼆种写法可以较⽅便的控制页⾯中其他组件。当然,第⼀种可以改写构造函数来实现。
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="schemas.huawei/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:text="Wait"
ohos:text_size="100"
/>
<Button
ohos:id="$+id:but1"
ohos:height="match_content"
ohos:width="match_content"
ohos:text="click me"
ohos:text_size="100"
ohos:background_element="red"
/>
active alignment</DirectionalLayout>
import t.Intent;
import ohos.agpponents.Button;
import ohos.agpponents.Component;
import ohos.agpponents.Text;
public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener { Text text1 = null;
@Override
public void onStart(Intent intent) {
super.setUIContent(ResourceTable.Layout_ability_main);
Button but1 = (Button)findComponentById(ResourceTable.Id_but1);
text1 = (Text) findComponentById(ResourceTable.Id_text1);
but1.setClickedListener(this);
}
@Override
public void onActive() {
}
@Override
public void onForeground(Intent intent) {
}
@Override
public void onClick(Component component){
Button but = (Button)component;
text1.setText("Clicked");
but.setText("Clicked");
}
}
第三种写法
匿名内部类
只能使⽤⼀次,且外部⽆法得知。
import t.Intent;
import ohos.agpponents.Button;
import ohos.agpponents.Component;
public class MainAbilitySlice extends AbilitySlice{
@Override
public void onStart(Intent intent) {
super.setUIContent(ResourceTable.Layout_ability_main);
Button but1 = (Button)findComponentById(ResourceTable.Id_but1); but1.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
Button but = (Button)component;
but.setText("Clicked");
}
});
}
@Override
public void onActive() {
}
@Override
public void onForeground(Intent intent) {
}
}
第四种写法
⽅法引⽤
需要同接⼝⽅法⼀致,⽤引⽤的⽅法作为接⼝的实现
onClick
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论