今天来学最常用到也最实用的Button按钮元件
我们经常需要点击按钮来做某个或多个事件
这时就需要设计按钮的触发事件
把点击後希望做的动作,全部写在按钮触发事件内
走起
按钮的触发事件也有分很多种
这边就学比较常用到的2种事件
1.按一下事件
2.长按事件
这触发事件就是某些动作会在点一下按钮後执行
目标:点一下按钮,在Logcat显示"按一下"
<?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>
<?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>
Log.v("one","点一下" );
one是Tag,等等只要在Logcat搜寻one,很快就看到了
执行结果:
点Button後,在Android Studio里的Logcat搜寻one
今天就到这里拉
明天来学长按事件
各位掰掰!!!
上架 一.打包APK 1.选Buile-Generate Signed Bundle/APK 2.选...
今天给大家分享9个Excel语法常用的公式,可以有效的解决Excel中办公所需,0基础也可以轻松学会...
本文章同时发布於: Medium iT 邦帮忙 大家好,继上次Week39 - 各种安全性演算法的应...
连假就让我轻松点吧XDD,虽然平常内容也没有多到哪去哈哈。 If you are on ...
大家午安 ~ 刚刚打开介面发文时,看到有 iThome 邦友订阅文章,真的是无比开心 Q 感谢大家的...