Day11 - Button(一)

今天来学最常用到也最实用的Button按钮元件
我们经常需要点击按钮来做某个或多个事件
这时就需要设计按钮的触发事件
把点击後希望做的动作,全部写在按钮触发事件内
走起

开始

按钮的触发事件也有分很多种
这边就学比较常用到的2种事件
1.按一下事件
2.长按事件

按一下事件

这触发事件就是某些动作会在点一下按钮後执行
目标:点一下按钮,在Logcat显示"按一下"

  • 先把原本的TextView删掉,拉一个Button按钮出来,按钮的字改"按一下"
    https://ithelp.ithome.com.tw/upload/images/20210917/20141769K7rCAVjKSS.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/button"
        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>
  • 设定按钮id
    这里就简单的把id设btn_1
<?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>
  • 新增按钮点一下的事件
    一样先宣告按钮,并透过findViewById抓到按钮元件
    https://ithelp.ithome.com.tw/upload/images/20210917/20141769pxezvwCaZk.png
    之後新增setOnClickListener监听事件
    https://ithelp.ithome.com.tw/upload/images/20210917/20141769wH2ZzqtjqV.png
    再来就可以在里面写你要的动作
    这边就在Logcat里显示"按一下"
Log.v("one","点一下" );

one是Tag,等等只要在Logcat搜寻one,很快就看到了
https://ithelp.ithome.com.tw/upload/images/20210917/20141769fNeDWfjE3w.png
执行结果:
点Button後,在Android Studio里的Logcat搜寻one
https://ithelp.ithome.com.tw/upload/images/20210917/20141769V4h6sPFdcD.png


今天就到这里拉
明天来学长按事件

各位掰掰!!!


<<:  Day16 支持向量机实作

>>:  [DAY 7] Spring Boot 启动原理

第31天~上架

上架 一.打包APK 1.选Buile-Generate Signed Bundle/APK 2.选...

零基础也能学会的9个Excel函数,进阶必备!

今天给大家分享9个Excel语法常用的公式,可以有效的解决Excel中办公所需,0基础也可以轻松学会...

Week40 - 各种安全性演算法的应用 - 窜改、抵赖实作 [高智能方程序系列]

本文章同时发布於: Medium iT 邦帮忙 大家好,继上次Week39 - 各种安全性演算法的应...

Day 25. VR菜单3

连假就让我轻松点吧XDD,虽然平常内容也没有多到哪去哈哈。       If you are on ...

【Day 22】 实作 - 如何在 AWS Quicksight Join 不同资料源

大家午安 ~ 刚刚打开介面发文时,看到有 iThome 邦友订阅文章,真的是无比开心 Q 感谢大家的...