一起Talk Android吧(第一百七十九回:Android中的Handler机制八)

各位看官们大家好,上一回中咱们说的是Android中Handler机制的例子,这一回咱们继续说该例子。闲话休提,言归正转。让我们一起Talk Android吧!

看官们,我们在前面章回中介绍了Handler机制的具体使用方法并且分析了Handler机制的源代码,只是没有自己动手来实现,有些看官都在摩拳擦掌,这一回中我们将通过代码结合文字的方式给大家演示如何使用Handler机制,这也是我们一惯的风格。

看官们,我们先创建一个工程,在工程只有一个Activity,使用Handler的逻辑都位于此Activity中。 不过使用Handler相关的结果都呈现到了画面中,因此需要修改布局文件的内容。下面是布局文件的代码,请大家参考:

<?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=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="This is the main thread"
        />
    <TextView
        android:id="@+id/id_tv_name_main_thread"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/id_tv_id_main_thread"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
       android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <Button
        android:id="@+id/id_bt_start_message"
        android:text="Sart Message"
        android:textAllCaps="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/id_bt_end_message"
        android:text="End Message"
        android:textAllCaps="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="This is the sub thread"
        />
    <TextView
        android:id="@+id/id_tv_name_sub_thread"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/id_tv_id_sub_thread"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/id_bt_show_name"
            android:text="Show Sub Thread Name"
            android:textAllCaps="false"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/id_bt_show_id"
            android:text="Show Sub Thread ID"
            android:textAllCaps="false"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>

从上面的代码中可以看到,布局文件主要分为两组,每组中使用三个TextView控件用来显示线程的标题,线程名称,线程ID;使用两个Button控件用来给消息队列中发送消息。第一组用来显示主线程相关的内容,第二组用来显示子线程相关的内容。布局文件整体使用线性布局,布局中的控件使用垂直排列,刚才所说的两组控件都位于该线性布局中。

各位看官,关于Android中Handler机制的例子咱们就介绍到这里,欲知后面还有什么例子,且听下回分解!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

talk_8

真诚赞赏,手有余香

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值