- This is the first day of my participation in the August More Text Challenge.
1. What is an event?
- Events are actions that can be identified. Actions that can be recognized by components such as text, buttons, images, etc.
- Common events are: click, double click, long press, and touch events.
- You can add different events to text, buttons, and so on. For example, after adding the click event, when we click the text and button again, we can run the corresponding code.
- Common events are:
2. Click events (Common)
- Click events: also known as click events. Is one of the most used events in development.
- Interface name: ClickedListener, also called: Click event.
- For example: when clicked, text content will send changes
3. Implementation steps
- Create project name:
ListenerApplication
ability_main.xml
<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:but1"
ohos:height="match_content"
ohos:width="match_content"
ohos:text="点我"
ohos:text_size="200"
ohos:background_element="red">
</Button>
</DirectionalLayout>
Copy the code
MainAbilitySlice
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;
public class MainAbilitySlice extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
//1. Find the button
// This.findComponentById(resourcetable.id_but1);
// This: object of this class, refers to: MainAbilitySlice (subinterface object)
// In the subinterface, find the corresponding component by id
// Call the method with this. This can be omitted
//findComponentById(ResourceTable.Id_but1);
// Return a component object (so the parent of the component)
// When we actually write code, we need to go down: strong
Component but1 = (Button) findComponentById(ResourceTable.Id_but1);
//2. Bind the button to a click event. When the button is clicked, the method in MyListener will be executed
// The method is the following click
but1.setClickedListener(new MyListener());
}
@Override
public void onActive(a) {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent); }}class MyListener implements Component.ClickedListener{
@Override
public void onClick(Component component) {
//Component: The parent of all components
// Component argument: the component object to be clicked, which in this case means your object
//component.setText(); SetText is a subclass-specific method that requires a downward cast: a strong cast
Button but = (Button) component;
but.setText("Tapped."); }}Copy the code
- Run:
- After the click:
4. Click events
- Click events: also known as click events. Is one of the most used events in development.
- Implementation steps:
1. Find the component by ID.
2. Set the click event for the button component.
3. Write a class that implements the ClickedListener interface and override the onClick method.
4. Write the onClick method body.