开启子线程的方法(二)

本文介绍如何通过Android的Handler机制实现一个实时更新显示时间的电子表应用。该应用利用Handler在主线程与子线程间传递消息,每秒更新时间显示。文章详细展示了XML布局设置与Java代码逻辑,并探讨了进一步的功能扩展。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第二个方法就是利用Handler开启子线程,这次一样举个例子,就用表吧,像电子表,几时几分几秒样式的,实现效果是这样的,

这个时间就是根据虚拟机上的系统时间一样,原理就是首先获取系统时间,然后将时间转化为字符串,由字节类型转化为int类型,最后传给控件
接着看代码吧
1.在xml文件先弄几个控件,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- 定义5个ImageView来显示液晶数字 -->
<ImageView
android:id="@+id/img01"  
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
/>
<ImageView
android:id="@+id/img02"  
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
/>
<ImageView
android:id="@+id/img03"  
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:src="@drawable/su00"
/>
<ImageView
android:id="@+id/img04"  
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
/>
<ImageView
android:id="@+id/img05"  
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
/>
<ImageView
android:id="@+id/img06"  
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:src="@drawable/su00" 
/>
<ImageView
android:id="@+id/img07"  
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
/>
<ImageView
android:id="@+id/img08"  
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
/>
</LinearLayout>
2.在java文件编辑
package com.example.myclick;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.ImageView;


public class MainActivity extends Activity {

private Timer timer = new Timer();

private Context context;
// 将0~9的液晶数字图片定义成数组
private int[] image = new int[]
{
R.drawable.su01,
R.drawable.su02,
R.drawable.su03,
R.drawable.su04,
R.drawable.su05,
R.drawable.su06,
R.drawable.su07,
R.drawable.su08,
R.drawable.su09,
R.drawable.su10,
};
// 将显示小时、分钟、秒钟的ImageView定义成数组
private int[] views = new int[]
    {
    R.id.img01,
    R.id.img02,
    R.id.img04,
    R.id.img05,
    R.id.img07,
    R.id.img08
    };

ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        timer = new Timer();
// 启动周期性调度
timer.schedule(new TimerTask()
{
public void run()
{
// 发送空消息,通知界面更新
handler.sendEmptyMessage(0111); 
}
}, 0, 1000);
        }
    private Handler handler = new Handler()
{
public void handleMessage(Message msg)
{
if (msg.what == 0111)
{

// 定义SimpleDateFormat对象
SimpleDateFormat df = new SimpleDateFormat("HHmmss");
// 将当前时间格式化成HHmmss的形式
String timeStr = df.format(new Date());
for(int i = 0 ; i < timeStr.length() ;i++)
{
// 将第i个数字字符转换为对应的数字
int num = timeStr.charAt(i) - 48;
// 将第i个图片的设为对应的液晶数字图片
img = (ImageView) findViewById(views[i]);
img.setImageResource(image[num]);
}

}
super.handleMessage(msg);
}
};
    
}

代码写到这里就完事了,这只是一个简单的代码,要是学会网络的话可以获取各个地的时间,还可以自己做天气预报,其实这些都差不多。嘿嘿,在这里请教一个问题,

不知道为啥,原先我写的那个一,它是在MarkDown编辑器里写的,但发表之后写的代码显示不全,你们这些高手看看是不是有个阅读全文之类的啊,反正我是没找到,求教委屈我可不想让它只显示一半啊,谢谢你们了!!!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值