Android自定义控件动态显示时间 年月日 时分秒
2018-01-10 15:26
971 查看
这个要求用的不多,但是也有写项目上面不想看手机时间,要求你直接把时间日期给动态显示在界面上 这里就记录一个自定义控件吧
我这个是直接截图静态的,有需要的话可以自己直接开个程序跑一下就知道了
布局的话
直接这样一个就行了 界面上不需要操作 会自动刷新
下面是文件下载:
文件类下载
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="" 9dc0 android:textSize="25sp" android:textStyle="bold"/>
直接这样一个就行了 界面上不需要操作 会自动刷新
下面是文件下载:
文件类下载
相关文章推荐
- Android动态显示当前年月日时分秒系统时间
- Android动态显示系统时间,附获得当前年月日星期时分秒方法,一步到位
- Android动态显示当前年月日时分秒系统时间(示例代码)
- js显示动态时间年月日时分秒
- JavaScript中获取时间【时间显示不到10自动加0】new Date()_年月日时分秒
- js动态显示时间 时分秒设置为红色
- Android 动态显示时间
- 在前台页面将时间戳改成年月日时分秒显示
- html中动态显示年月日星期时分秒
- android 动态的显示时间
- js显示动态年月日时分秒
- Android 用Time和Calendar获取系统当前时间源码分享(年月日时分秒周几)
- [sql],去掉时间类型的时分秒,只显示年月日
- androidの当前时间动态显示
- Android自定义控件View(实现控件的动画效果,自定义类画简略时钟,TextView中文字逐一显示,动态设置progress的圆环/扇形ProgressBar等)
- 解决ibatis查询时间只显示年月日,不显示时分秒的方法
- 时间操作(JavaScript版)—页面显示格式:年月日 上午下午 时分秒 星期
- android cavas 绘制坐标系并显示矩形波方波-横轴为当前时间动态更新(不用achartengine)
- JS 动态显示年月日星期时分秒
- Android中当前时间的动态显示