3.16

所花时间(包括上课):0.5

打码量(行):100

博客量(篇):1

了解到知识点:学习点击事件和长按点击事件

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent">

 

    <!-- Button -->

    <Button

        android:id="@+id/buttonClick"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Click Me"

        android:layout_centerInParent="true" />

 

</RelativeLayout>

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.Toast;

 

import androidx.appcompat.app.AppCompatActivity;

 

public class MainActivity extends AppCompatActivity {

 

    private Button buttonClick;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        // 初始化按钮

        buttonClick = findViewById(R.id.buttonClick);

 

        // 设置点击事件监听器

        buttonClick.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                // 处理按钮点击事件

                Toast.makeText(MainActivity.this, "Button Clicked!", Toast.LENGTH_SHORT).show();

            }

        });

    }

}

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.Toast;

 

import androidx.appcompat.app.AppCompatActivity;

 

public class MainActivity extends AppCompatActivity {

 

    private Button buttonClick;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        // 初始化按钮

        buttonClick = findViewById(R.id.buttonClick);

 

        // 设置点击事件监听器

        buttonClick.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                // 处理按钮点击事件

                Toast.makeText(MainActivity.this, "Button Clicked!", Toast.LENGTH_SHORT).show();

            }

        });

 

        // 设置长按点击事件监听器

        buttonClick.setOnLongClickListener(new View.OnLongClickListener() {

            @Override

            public boolean onLongClick(View v) {

                // 处理按钮长按事件

                Toast.makeText(MainActivity.this, "Button Long Clicked!", Toast.LENGTH_SHORT).show();

                return true; // 返回 true 表示事件已处理,不再触发点击事件

            }

        });

    }

}

posted @ 2024-03-16 12:34  赵千万  阅读(2)  评论(0编辑  收藏  举报