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

Count the number of clicks in 10 seconds

  • Click the button within a certain amount of time, and the number of times the button is clicked is recorded in the Text Text

  • Case realization:
  • Create a project: StatisticsApplication

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:background_element="$graphic:background_ability_main"
        ohos:layout_alignment="horizontal_center"
        ohos:text="$string:mainability_HelloWorld"
        ohos:text_size="40vp"
        />

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

    </Button>
</DirectionalLayout>
Copy the code

MainAbilitySlice

package com.xdr630.statisticsapplication.slice;

import com.xdr630.statisticsapplication.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 but1;

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

        // Find the component
        text1 = (Text) findComponentById(ResourceTable.Id_text1);
        but1 = (Button) findComponentById(ResourceTable.Id_but1);

        // Set the click event for the button
        but1.setClickedListener(this);

    }

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

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


    // If flag is true, the current button is clicked for the first time
    // If flag is false, the current button is not clicked for the first time
    boolean flag = true;
    long startTime = 0;

    // Count the number of clicks
    int count = 0;

    @Override
    public void onClick(Component component) {
        // Once clicked, the counter increases by itself
        count++;
        // Count things like 10s, and display the number of times in the text box
        if (flag){
            // If this is the first time to click the button, record the current time
            startTime = System.currentTimeMillis();
            // When the game starts after the first click, modify the text in the button
            but1.setText("Please be crazy about me.");
            When the onClick method is called the second time, the flag is false, indicating that the second time is not the first time, and the else code will be used
            flag = false;
        }else{
            if ((System.currentTimeMillis() - startTime) <= 10000){
                text1.setText(count + "");
            }else {
                but1.setText("The end");
                // Cancel the button click event so that the button can no longer be clicked
                but1.setClickable(false); }}}}Copy the code
  • Run:

  • And then you can’t click anymore
  • You can also extend this further by adding a reset button click event that can be clicked to restart the project when it is finished, without the need to restart the project