03常用组件和事件监听

常用组件

组件

Android SDK 提供了名为 android.widget 的包,其中提供了在应用程序界面设计中大部分常用的 UI 可视组件,如文本框、按钮。

如文单选按钮、按钮、文本框:activity_main.xml

<?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"
    android:gravity="center_horizontal">

    <RadioGroup
        android:id="@+id/btn_sex"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RadioButton
            android:id="@+id/btn_male"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="male"
            />
        <RadioButton
            android:id="@+id/btn_female"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="female"
            />
    </RadioGroup>
    <Button
        android:id="@+id/btn_check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示"
        />
    <TextView
        android:id="@+id/text_show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
        />

</LinearLayout>

事件

事件就是,单机组件、滑动屏幕、双击组件等等。

事件处理有两种方法,一种是直接在组件上设置处理方法,一种是设置监听程序。

第一种:组件绑定事件

直接在组件上添加处理方法:

  1. 组件上绑定

  2. 在MainActivity.java文件中定义showMessage方法

  3. 启动虚拟手机,启动程序。单机按钮,文字改变。

第二种:监听组件事件

  1. 不用组件绑定事件

  2. 在MainActivity.java中,给组件添加监听

  3. 启动程序,在虚拟手机中显示如下:

原文地址:https://www.cnblogs.com/mingriyingying/p/14371729.html