android studio 按钮点击事件的实现方法(三种方法)

我是小白,刚学编程没多久,完全自学,这些也是在网上看的,加上自己总结,如有错误请指正。

方法1:在布局文件中给需要单击事件的按钮添加一个onClick属性。如下图:

 再在MainActivity.java里添加实现代码,如:

public void changeStr(View view) {
        textView.setText("按了第1个按钮。");

 这个方法适合单个按钮,而且我觉得这个方法好像比较好理解,跟其他编程语文实现按钮功能差不多。

全部代码:

方法2:在绑定控件ID的时候,建一个View.OnClickListener(),并传入setOnClickListener方法。 其实也就是用onclicklistener实现,我对这个理解的不太好。这个方法不用改布局文件,适用单个按钮。代码写在oncreate中。

findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                textView.setText("按了第2个按钮");
            }
        });

方法1、2例程:

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="300dp"
        android:text="Hello World!"

        android:textSize="40dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="changeStr"
        android:text="onclick" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:text="listener" />

</FrameLayout>

 MainActivity.java

package com.example.button;

import androidx.appcompat.app.AppCompatActivity;

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

import java.io.DataOutputStream;

public class MainActivity extends AppCompatActivity  {
    private TextView textView;
    private Button button1;
    private Button button2;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button1=(Button)findViewById(R.id.button1);
        textView=(TextView)findViewById(R.id.textView);

       findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                textView.setText("按了第2个按钮");
            }
        });
    }

    public void changeStr(View view) {
        textView.setText("按了第1个按钮。");
    }
}

 方法3:为每个控件绑定id,再重写的onClick中用swich判断id实现代码功能。这个方法适合多个按钮,代码简单,逻辑清晰。

1.在oncreate中为控件绑定方法:

        findViewById(R.id.button1).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
        findViewById(R.id.button3).setOnClickListener(this);

然后再在this上按alt+enter,调出下图,第2项回车后会再弹出一个窗口,再回车。我只知道这个补全代码,到底怎么回事我也不知道。

2.重写onclick.

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button1:
                textView.setText("按了第1个按钮");
                break;
            case R.id.button2:
                textView.setText("按了第2个按钮");
                break;
            case R.id.button3:
                textView.setText("按了第3个按钮");
                break;
        }
    }

方法3例程:

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="300dp"
        android:text="Hello World!"

        android:textSize="40dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       
        android:text="onclick" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:text="listener" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:text="重写onclick" />

</FrameLayout>

MainActivity.java

package com.example.button;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.DataOutputStream;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView textView;
    private Button button1;
    private Button button2;
    private Button button3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);       
        findViewById(R.id.button1).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
        findViewById(R.id.button3).setOnClickListener(this);
        textView=(TextView)findViewById(R.id.textView);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button1:
                textView.setText("按了第1个按钮");
                break;
            case R.id.button2:
                textView.setText("按了第2个按钮");
                break;
            case R.id.button3:
                textView.setText("按了第3个按钮");
                break;
        }
    }
}
2024.11.12补充

在新版本as上(版本2023.3.1),版本图:

方法3可能出问题,各种错误,如下图,问题原因不知道,反正就是直接复制代码无法运行。

 

解决方法:

1.解决 case R.id.button1:的问题

打开gradle.properties,并添加如下代码:android.nonFinalResIds=false 如图:

再点击上面的sync now就可解决。

网友说是因,使用JDK17以上版本,switch语句报错:Constant expression required,原因不知道。

2.解决其他问题:定位到显示有错误的this行。

按alt+enter键,显示如图,点击红框位置:

点击后显示如图,再点击红框位置:

直接点ok,问题解决。

两个地方增加了代码:

位置1:

位置2:

位置2代码可以删除,不影响功能。

加这两部分代码有没有后果不知道,删除位置2代码有没有后果也不知道。

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kim5659

你的鼓励是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值