【Android】触发按钮的三种方式

方法1 通过id绑定点击监听

实现
findViewById(R.id.home_button).setOnClickListener(
    new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "点击按钮", Toast.LENGTH_LONG).show();
        }
    }
);
源码
  • app/src/main/java/com/yyshu/demo1/MainActivity.java
package com.yyshu.demo1;

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);
        findViewById(R.id.home_button).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity.this, "点击按钮", Toast.LENGTH_LONG).show();
                    }
                }
        );
    }
}

方法2 通过android:onClick绑定

注:需要xml视图层增加android:onClick="button_test1"

public void button_test1(View view) {
    Toast.makeText(MainActivity.this, "点击按钮2", Toast.LENGTH_LONG).show();
}

方法3 继承View.OnClickListener,所有按钮通过onClick触发

注:需要implements View.OnClickListener继承

package com.yyshu.demo1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mButton = (Button) findViewById(R.id.home_button);
        mButton.setOnClickListener(this);
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.home_button:
                Toast.makeText(MainActivity.this, "点击按钮3", Toast.LENGTH_LONG).show();
                break;
        }
    }
}

视图UI层

  • app/src/main/res/layout/activity_main.xml
<?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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/home_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="@string/home_title"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <Button
        android:id="@+id/home_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="@string/home_bitton"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/home_title"
        app:layout_constraintVertical_bias="0.0" />

</androidx.constraintlayout.widget.ConstraintLayout>
  • app/src/main/res/values/strings.xml
<resources>
    <string name="app_name">DEMO1</string>
    <string name="home_title">你好世界!</string>
    <string name="home_bitton">按钮</string>
</resources>
上一篇:解决android10(API 29)中NetworkInfo被废弃的问题


下一篇:Mr.Alright---安卓Q R 原生Launcher 偶尔无法添加小部件