Android自定义控件动态显示时间 年月日 时分秒
程序员文章站
2024-01-23 18:08:52
...
直接把时间日期给动态显示在界面上 这里就记录一个自定义控件吧
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.get(Calendar.DAY_OF_WEEK));//获取星期
if ("1".equals(mWay)) {
mWay = "天";
} else if ("2".equals(mWay)) {
mWay = "一";
} else if ("3".equals(mWay)) {
mWay = "二";
} else if ("4".equals(mWay)) {
mWay = "三";
} else if ("5".equals(mWay)) {
mWay = "四";
} else if ("6".equals(mWay)) {
mWay = "五";
} else if ("7".equals(mWay)) {
mWay = "六";
}
String tiems = time + "星期" + mWay;
Log.e(TAG, "run: " + time + "1231231");
handler.sendMessage(handler.obtainMessage(100, tiems));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
runnable.start();
}
//根据毫秒时间获取格式化的提示 按自己实际要求来写
private String convertTimeToFormat(long timeMills) {
long curTime = Calendar.getInstance().getTimeInMillis();
long time = (curTime - timeMills) / (long) 1000;//已经将单位转换成秒
if (time > 0 && time < 60) {
return "刚刚";
} else if (time >= 60 && time < 3600) {
return time / 60 + "分钟前";
} else if (time >= 3600 && time < 3600 * 24) {
return time / 3600 + "小时前";
} else if (time >= 3600 * 24 && time < 3600 * 24 * 30) {
return time / 3600 / 24 + "天前";
} else if (time >= 3600 * 24 * 30 && time < 3600 * 24 * 30 * 12) {
return time / 3600 / 24 / 30 + "个月前";
} else if (time >= 3600 * 24 * 30 * 12) {
return time / 3600 / 24 / 30 / 12 + "年前";
} else {
return "刚刚";
}
}
}
效果图就看下面
我这个是直接截图静态的,有需要的话可以自己直接开个程序跑一下就知道了
布局的话
<com.ewininfo.testproject.UpdateTimeTextView
android:id="@+id/up_time"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center"
android:text=""
android:textSize="25sp"
android:textStyle="bold"/>
直接这样一个就行了 界面上不需要操作 会自动刷新
转载:https://blog.csdn.net/qq_32937131/article/details/79024028
上一篇: 打印机换墨后不能打印红灯闪烁该怎么办?