Day12 - Button(二)

我们昨天讲到
按钮事件有分很多种
1.点击一下事件
2.长按事件
上一篇我们已经学会了按钮的点击一下事件
今天我们要更进阶一咪咪
来学第2个按钮的事件
按钮长按事件
长按这功能也很常用的到
比如:计算机长按归零、长按预览讯息
这些都是常按的范畴
那我们废话不多说

开始

今天我们一样搭配Log来验证长按功能是否正确

  • 布局的方面与昨天一样拉一个按钮出来
    按钮文字要打什麽都可以,我这边就直接打长按
    id也顺便设定好,我设跟昨天一样btn_1
    https://ithelp.ithome.com.tw/upload/images/20210918/20141769p33CDNDVCt.png
<?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">

    <Button
        android:id="@+id/btn_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="158dp"
        android:layout_marginTop="237dp"
        android:layout_marginEnd="159dp"
        android:text="长按"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
  • 新增按钮长按的事件
    跟昨天一样宣告Button,抓元件
    不一样的就是setOnClickListener事件
    改成setOnLongClickListener事件
    以及
    把Log.v("one","点一下")的内容改成Log.v("one","长按")
Log.v("one","长按" );

https://ithelp.ithome.com.tw/upload/images/20210918/20141769S5dEPNobjE.png

执行结果:
你会发现按一下没有反应,要按住不放Logcat才会显示one : 长按
https://ithelp.ithome.com.tw/upload/images/20210918/201417692YyW96okVp.png


<<:  电子书阅读器上的浏览器 [Day23] 双视窗可拖拉调整大小元件

>>:  分散式资料库:一致性协定

如何在 Android 和 iOS 设备上启用 YouTube 後台播放(不用下载)-2022

今天教大家一个简单免费的方式来享受YouTube後台播放。 只需 3 个简单的步骤,即可在 iOS ...

Grid笔记

假使设定HTML: <div class="container"> ...

Day 04-Azure介绍

在上一篇我们看到,即便我们能不写程序就设定一些自动回覆,仍然相当麻烦,如果需要的功能更多,更无法应付...

【PHP 设计模式大头菜】模板方法 Template Method

模板方法 Template Method 模板方法,是一种如果这包水泥我有、你也有,就连乔瑟夫都有...

Day25:Dynamic Programming(DP) - 动态规划(上)

在认识动态规划之前先来理解Divide and Conquer(分治法)吧!Divide and ...