• This is the fifth day of my participation in the August More Text Challenge.

1. Multiple buttons are clicked

  • New project: ListenerApplication5
  • Implementation code:

ability_main


      
<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:text1"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:text="Text"
        ohos:text_size="100">
    </Text>

    <Button
        ohos:id="$+id:login"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:text="Login"
        ohos:text_size="100"
        ohos:background_element="cyan">
    </Button>

    <Button
        ohos:id="$+id:register"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:text="Registered"
        ohos:text_size="100"
        ohos:background_element="red"
        >

    </Button>

</DirectionalLayout>
Copy the code

MainAbilitySlice

package com.xdr630.listenerapplication5.slice;

import com.xdr630.listenerapplication5.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 text1;
    Button login;
    Button register;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        //1. Find the text, button components
        text1  = (Text) findComponentById(ResourceTable.Id_text1);
        login = (Button) findComponentById(ResourceTable.Id_login);
        register = (Button) findComponentById(ResourceTable.Id_register);

        //2. Bind events to buttons
        // What to do with which component?
        // Click on the login button and register button
        login.setClickedListener(this);
        register.setClickedListener(this);
    }

    @Override
    public void onActive(a) {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        // Make a judgment first
        // Determine whether the current click button is the login button or the register button
        //component: indicates the currently clicked component
        if (component == login){
            // Indicates that the login button is clicked
            text1.setText("Click the login button.");
        }else if (component == register){
            // Click the register button
            text1.setText("Click the register button."); }}}Copy the code
  • Run:

  • Click the Login button:

  • Click the register button:

2. Section

  • To write to login logic or registration logic later, the overall architecture is similar to this example, the only difference is to change the setText content into the actual login logic or registration logic.