资源简介
安卓动态时间自定义控件,可以直接获取当前时间 日期年份,实时刷新
代码片段和文件信息
package com.ewininfo.testproject;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.util.Log;
import java.text.SimpleDateFormat;
import java.util.Calendar;
/**
* 创建者: 高石
* 版权:
* 描述: .
* Created by gsh on 2018/1/5.
*/
public class UpdateTimeTextView extends AppCompatTextView {
private String TAG = “UpdateTimeTextView“;
private Thread runnable;
private boolean mBoolean = true;
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
UpdateTimeTextView.this.setText((String) msg.obj);
}
};
private String DEFAULT_TIME_FORMAT = “yyyy-MM-dd HH:mm:ss “;
public UpdateTimeTextView(Context context) {
super(context);
Log.e(TAG “UpdateTimeTextView: 1“);
}
public UpdateTimeTextView(Context context AttributeSet attrs) {
super(context attrs);
init();//加载方法
Log.e(TAG “UpdateTimeTextView: 2“);
}
public UpdateTimeTextView(Context context AttributeSet attrs int defstyleAttr) {
super(context attrs defstyleAttr);
Log.e(TAG “UpdateTimeTextView: 3“);
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mBoolean = false;
Log.e(TAG “onDetachedFromWindow:关闭循环 “);
}
/**
* 更新时间
*/
private void init() {
runnable = new Thread() {
@Override
public void run() {
while (mBoolean) {
Calendar mCalendar = Calendar.getInstance();
SimpleDateFormat dateFormatter = new SimpleDateFormat(DEFAULT_TIME_FORMAT);
String time = dateFormatter.format(Calendar.getInstance().getTime());//获取当前时间
String mWay = String.valueOf(mCalendar.
评论
共有 条评论