一、常用控件button简介
button属于android.Widget包,并且继承了android.Widget.TextView类,提供了按钮控制的功能。
二、四种点击方式简介
1、使用匿名内部类创建点击事件
2、使用内部类创建点击事件
3、使用View.OnClickListener接口
4、使用button的xml属性 android:onClick
三、四种点击方式详细介绍
1、使用匿名内部类创建点击事件
这是十分常用的方式,并且也是步骤最简单的方式。
先在xml文件中直接创建4个按钮控件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:id="@+id/main_button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="按钮1"
/>
<Button
android:id="@+id/main_button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="按钮2"
/>
<Button
android:id="@+id/main_button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="按钮3"
/>
<Button
android:id="@+id/main_button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="按钮4"
/>
</LinearLayou