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


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

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

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

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

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

    全部代码:

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

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

    方法1、2例程:

    布局文件:

    1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. xmlns:app="http://schemas.android.com/apk/res-auto"
    3. xmlns:tools="http://schemas.android.com/tools"
    4. android:layout_width="match_parent"
    5. android:layout_height="match_parent"
    6. tools:context=".MainActivity">
    7. <TextView
    8. android:id="@+id/textView"
    9. android:layout_width="wrap_content"
    10. android:layout_height="wrap_content"
    11. android:layout_marginTop="300dp"
    12. android:text="Hello World!"
    13. android:textSize="40dp" />
    14. <Button
    15. android:id="@+id/button1"
    16. android:layout_width="wrap_content"
    17. android:layout_height="wrap_content"
    18. android:onClick="changeStr"
    19. android:text="onclick" />
    20. <Button
    21. android:id="@+id/button2"
    22. android:layout_width="wrap_content"
    23. android:layout_height="wrap_content"
    24. android:layout_marginTop="50dp"
    25. android:text="listener" />
    26. FrameLayout>

     MainActivity.java

    1. package com.example.button;
    2. import androidx.appcompat.app.AppCompatActivity;
    3. import android.os.Bundle;
    4. import android.view.View;
    5. import android.widget.Button;
    6. import android.widget.TextView;
    7. import android.widget.Toast;
    8. import java.io.DataOutputStream;
    9. public class MainActivity extends AppCompatActivity {
    10. private TextView textView;
    11. private Button button1;
    12. private Button button2;
    13. @Override
    14. protected void onCreate(Bundle savedInstanceState) {
    15. super.onCreate(savedInstanceState);
    16. setContentView(R.layout.activity_main);
    17. button1=(Button)findViewById(R.id.button1);
    18. textView=(TextView)findViewById(R.id.textView);
    19. findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
    20. public void onClick(View v) {
    21. textView.setText("按了第2个按钮");
    22. }
    23. });
    24. }
    25. public void changeStr(View view) {
    26. textView.setText("按了第1个按钮。");
    27. }
    28. }

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

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

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

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

     

     

    2.重写onclick.

    1. @Override
    2. public void onClick(View view) {
    3. switch (view.getId()){
    4. case R.id.button1:
    5. textView.setText("按了第1个按钮");
    6. break;
    7. case R.id.button2:
    8. textView.setText("按了第2个按钮");
    9. break;
    10. case R.id.button3:
    11. textView.setText("按了第3个按钮");
    12. break;
    13. }
    14. }

    方法3例程:

    布局文件:

    1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. xmlns:app="http://schemas.android.com/apk/res-auto"
    3. xmlns:tools="http://schemas.android.com/tools"
    4. android:layout_width="match_parent"
    5. android:layout_height="match_parent"
    6. tools:context=".MainActivity">
    7. <TextView
    8. android:id="@+id/textView"
    9. android:layout_width="wrap_content"
    10. android:layout_height="wrap_content"
    11. android:layout_marginTop="300dp"
    12. android:text="Hello World!"
    13. android:textSize="40dp" />
    14. <Button
    15. android:id="@+id/button1"
    16. android:layout_width="wrap_content"
    17. android:layout_height="wrap_content"
    18. android:text="onclick" />
    19. <Button
    20. android:id="@+id/button2"
    21. android:layout_width="wrap_content"
    22. android:layout_height="wrap_content"
    23. android:layout_marginTop="50dp"
    24. android:text="listener" />
    25. <Button
    26. android:id="@+id/button3"
    27. android:layout_width="wrap_content"
    28. android:layout_height="wrap_content"
    29. android:layout_marginTop="100dp"
    30. android:text="重写onclick" />
    31. FrameLayout>

    MainActivity.java

    1. package com.example.button;
    2. import androidx.appcompat.app.AppCompatActivity;
    3. import android.os.Bundle;
    4. import android.view.View;
    5. import android.widget.Button;
    6. import android.widget.TextView;
    7. import android.widget.Toast;
    8. import java.io.DataOutputStream;
    9. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    10. private TextView textView;
    11. private Button button1;
    12. private Button button2;
    13. private Button button3;
    14. @Override
    15. protected void onCreate(Bundle savedInstanceState) {
    16. super.onCreate(savedInstanceState);
    17. setContentView(R.layout.activity_main);
    18. findViewById(R.id.button1).setOnClickListener(this);
    19. findViewById(R.id.button2).setOnClickListener(this);
    20. findViewById(R.id.button3).setOnClickListener(this);
    21. textView=(TextView)findViewById(R.id.textView);
    22. }
    23. @Override
    24. public void onClick(View view) {
    25. switch (view.getId()){
    26. case R.id.button1:
    27. textView.setText("按了第1个按钮");
    28. break;
    29. case R.id.button2:
    30. textView.setText("按了第2个按钮");
    31. break;
    32. case R.id.button3:
    33. textView.setText("按了第3个按钮");
    34. break;
    35. }
    36. }
    37. }

     

  • 相关阅读:
    [附源码]计算机毕业设计旅游网的设计与实现Springboot程序
    协作乐高 All In One:DAO工具大全
    ALSA project the C library reference (ALSA工程 C库参考说明)
    创建git分支命名原则
    硬实力+软实力!2023功能测试进阶之路!
    soildworks2022如何快速更改草图基准面?
    OpenLDAP支持SAMBA特性
    [附源码]计算机毕业设计JAVAjsp零食销售系统
    用了 TCP 协议,就一定不会丢包吗?
    学校介绍静态HTML网页设计作品 DIV布局学校官网模板代码 DW大学网站制作成品下载 HTML5期末大作业
  • 原文地址:https://blog.csdn.net/kim5659/article/details/126327208