• Android Button四种点击事件和长按事件


    项目XML代码

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout
     3     xmlns:android="http://schemas.android.com/apk/res/android"
     4     xmlns:tools="http://schemas.android.com/tools"
     5     android:layout_width="match_parent"
     6     android:layout_height="match_parent"
     7     android:orientation="vertical"
     8     tools:context=".MainActivity">
     9 
    10     <Button
    11         android:layout_width="match_parent"
    12         android:layout_height="wrap_content"
    13         android:layout_marginTop="10dp"
    14         android:onClick="doClick"
    15         android:text="xml添加doClock"/>
    16 
    17     <Button
    18         android:id="@+id/button2"
    19         android:layout_width="match_parent"
    20         android:layout_height="wrap_content"
    21         android:layout_marginTop="10dp"
    22         android:text="匿名内部类点击"/>
    23 
    24     <Button
    25         android:id="@+id/button3"
    26         android:layout_width="match_parent"
    27         android:layout_height="wrap_content"
    28         android:layout_marginTop="10dp"
    29         android:text="自定义点击事件"/>
    30 
    31     <Button
    32         android:id="@+id/button4"
    33         android:layout_width="match_parent"
    34         android:layout_height="wrap_content"
    35         android:layout_marginTop="10dp"
    36         android:text="继承方式"/>
    37 
    38     <Button
    39         android:id="@+id/button5"
    40         android:layout_width="match_parent"
    41         android:layout_height="wrap_content"
    42         android:layout_marginTop="10dp"
    43         android:text="长按点击事件"/>
    44 
    45 </LinearLayout>

    JAVA代码:

     1 package com.example.a11658.buttondemo;
     2 
     3 import android.support.v7.app.AppCompatActivity;
     4 import android.os.Bundle;
     5 import android.view.View;
     6 import android.widget.Button;
     7 import android.widget.Toast;
     8 
     9 public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    10     Button button2, button3, button4, button5;
    11 
    12     @Override
    13     protected void onCreate(Bundle savedInstanceState) {
    14         super.onCreate(savedInstanceState);
    15         setContentView(R.layout.activity_main);
    16 
    17         initView();
    18     }
    19 
    20     private void initView() {
    21         button2 = findViewById(R.id.button2);
    22         button3 = findViewById(R.id.button3);
    23         button4 = findViewById(R.id.button4);
    24         button5 = findViewById(R.id.button5);
    25 
    26         button2.setOnClickListener(new View.OnClickListener() {
    27             @Override
    28             public void onClick(View v) {
    29                 //匿名内部类实现点击事件
    30                 Toast.makeText(MainActivity.this, "匿名内部类的点击事件", Toast.LENGTH_SHORT).show();
    31             }
    32         });
    33 
    34         button3.setOnClickListener(new MyListener());
    35         button4.setOnClickListener(this);
    36         button5.setOnLongClickListener(new View.OnLongClickListener() {
    37             @Override
    38             public boolean onLongClick(View v) {
    39                 Toast.makeText(MainActivity.this, "长按事件", Toast.LENGTH_SHORT).show();
    40                 return false;
    41             }
    42         });
    43     }
    44 
    45     public void doClick(View view) {
    46         //xml通过onClick实现点击事件
    47         Toast.makeText(this, "xml点击实现", Toast.LENGTH_SHORT).show();
    48     }
    49 
    50     @Override
    51     public void onClick(View v) {
    52         //继承方式
    53         Toast.makeText(this, "继承点击", Toast.LENGTH_SHORT).show();
    54     }
    55 
    56     class MyListener implements View.OnClickListener {
    57 
    58         @Override
    59         public void onClick(View v) {
    60             //自定义方式
    61             Toast.makeText(MainActivity.this, "自定义点击事件", Toast.LENGTH_SHORT).show();
    62         }
    63     }
    64 }
    备注:Button数量不多的情况下推荐使用第二种,匿名内部类的方式实现;反之则推荐使用第四种,Activity继承View.OnClickListener实现

    代码链接:https://github.com/1165863642/ButtonDemo
  • 相关阅读:
    NGUI Sprite 和 Label 改变Layer 或父物体后 未更新深度问题
    unity销毁层级物体及 NGUI 深度理解总结
    unity 2d 和 NGUI layer
    关于NGUI与原生2D混用相互遮盖的问题心得
    CentOS7为firewalld添加开放端口及相关操作
    Python 操作redis有序集合(sorted set)
    win10下安装redis 服务
    python2/3中 将base64数据写成图片,并将图片数据转为16进制数据的方法、bytes/string的区别
    解决最小化安装Centos7后无法上网的问题,以及安装成功后的基本配置
    在mysql中使用group by和order by取每个分组中日期最大一行数据,亲测有效
  • 原文地址:https://www.cnblogs.com/MrChen-/p/10303288.html
Copyright © 2020-2023  润新知