Android studio Button 监听敲击事件

1,在布局实现

我们在layout文件中,给每一个用到的Button设置属性android:onClick="onClick",  然后我们在MainActivity 里面写一个onClick()方法,这里就不是重写了,因为我们没有任何继承父类和引用接口,这里的方法名可以随意取。然后写上代码逻辑。完整代码如下:

activity_main.xml文件内容如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="学习安卓,你准备好了吗"
        android:id="@+id/tv_android"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="准备好了"
        android:id="@+id/bt_android"
        android:onClick="Welcome"/>##设置点击事件按钮方法为Welcome
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="没有准备好"
        android:id="@+id/bt_android_1"
        android:onClick="noway"/>##设置点击事件按钮方法为noway
</LinearLayout>

MainActivity文件如下:

package com.unity3d.myapplication1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

/*
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}*/
public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void Welcome(View view) {

        Toast.makeText(this, "欢迎来到安卓世界", Toast.LENGTH_SHORT).show();
    }
    public void noway(View view) {

        Toast.makeText(this, "bye bye bye ", Toast.LENGTH_SHORT).show();
    }



}

二、接口实现

  第二种方法只要引用View.OnClickListener这个接口就行,接着Button button=findViewById(R.id.button);用来声明和绑定button控件,button.setOnClickListener(this);设置button的监听器,这两者缺一不可。下面就是重写onClick()方法,一般使用switch语句,参数是view,可以根据不同id来赋予不同的点击事件,不用像上面匿名内部类那样每一个按钮都要单独设置一下点击事件。所有代码如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <Button
        android:id="@+id/button_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="测试按键"
        android:textSize="25sp"/>

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="11111"
        android:textSize="25sp"/>

    <Button
        android:id="@+id/button_2"
        android:layout_width="match_parent"
        android:layout_height="43dp"
        android:text="ok ok ok "
        android:textSize="25sp"


        />
</LinearLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>

package com.unity3d.myapplication1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.widget.Button;
import android.util.Log;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
    Button TestButton1, TestButton2; //创建button
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TestButton1 = findViewById(R.id.button_1); //通过id找到对应button
        TestButton2 = findViewById(R.id.button_2);
        TestButton1.setOnClickListener(new mButtonListener());
        TestButton2.setOnClickListener(new mButtonListener());
    }
    //新建mButtonListener类申明使用OnClickListener接口
    public class mButtonListener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.button_1: //按键1
                    Toast.makeText(MainActivity.this,"Hello world",Toast.LENGTH_LONG).show();

                    Log.d("button", "onClick: 1");
                    break;
                case R.id.button_2: //按键2
                    Toast.makeText(MainActivity.this,"ok ok ok ok ",Toast.LENGTH_LONG).show();
                    Log.d("button", "onClick: 2");
                    break;
                default: break;
            }
        }
    }
}

相关推荐

  1. Android studio Button 监听敲击事件

    2024-05-01 23:04:01       31 阅读
  2. 文件系统事件监听

    2024-05-01 23:04:01       43 阅读
  3. python --监听鼠标事件

    2024-05-01 23:04:01       29 阅读
  4. spring监听事件

    2024-05-01 23:04:01       31 阅读
  5. sqlalchemy事件监听

    2024-05-01 23:04:01       19 阅读
  6. Spring事件发布与监听

    2024-05-01 23:04:01       44 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-01 23:04:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-01 23:04:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-01 23:04:01       87 阅读
  4. Python语言-面向对象

    2024-05-01 23:04:01       96 阅读

热门阅读

  1. OneFlow概念清单

    2024-05-01 23:04:01       35 阅读
  2. sql连续登录

    2024-05-01 23:04:01       29 阅读
  3. SQL:SUBSTR函数的基本介绍

    2024-05-01 23:04:01       34 阅读
  4. python 笔记:cls VS self

    2024-05-01 23:04:01       32 阅读
  5. Linux 第十六章

    2024-05-01 23:04:01       33 阅读
  6. Day27-Linux系统服务管理知识2

    2024-05-01 23:04:01       30 阅读
  7. 基于C8051F340单片机的舵机简单控制程序

    2024-05-01 23:04:01       31 阅读
  8. MongoDB聚合运算符:$sqrt

    2024-05-01 23:04:01       33 阅读
  9. 笨蛋学C++之 C++对数据库实现CRUD

    2024-05-01 23:04:01       37 阅读
  10. vue3父组件调用子组件方法

    2024-05-01 23:04:01       33 阅读
  11. 如何将API 中的excel 文件load 到 Azure blob 中

    2024-05-01 23:04:01       34 阅读
  12. .requires_grad,.detach(),torch.no_grad()

    2024-05-01 23:04:01       29 阅读
  13. C/C++中的整数除法运算与汇编指令DIV和IDIV

    2024-05-01 23:04:01       35 阅读
  14. 如何看待AIGC技术

    2024-05-01 23:04:01       29 阅读