Android开发实例——倒计时器——Android多线程编程


一、倒计时器功能描述

这里我们通过倒计时器的案例来讲解Android多线程编程。
其中:
(1)子线程用于计时,一秒钟向主线程发送一次消息。
(2)主线程用于接收消息并更新UI控件——TEXTVIEW

二、实现步骤

1.新建Activity,命名为ThreadDemo

在这里插入图片描述

2.设计ThreadDemo 的布局文件

在这里插入图片描述

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

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="43dp"
        android:layout_marginTop="100dp"
        android:text="TextView"
        android:textColor="@color/colorAccent"
        android:textSize="32sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="92dp"
        android:text="开始倒计时"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView3" />

</androidx.constraintlayout.widget.ConstraintLayout>

3.编写ThreadDemo.java 文件代码

3.1 定义全局变量

下面展示一些 内联代码片,注意全局变量放置位置
在这里插入图片描述

    private final int UPDATE_TEXT =0x03;
    private final int UPDATE_TIME =0x04;

    boolean isStop = false;
    private int number =6;
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值