Button控件表示按钮,通过用户点击来执行操作,当Button控件被点击时会触发点击效果。
一、Button按钮控件设置点击事件
为Button按钮控件设置点击事件的方式主要有三种方式:
(1)在布局文件中在按钮中添加onClick属性
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".ButtonThree">
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="Click"
android:text="点击按钮"
/>
</LinearLayout>