Android动态显示当前年月日时分秒系统时间(示例代码)

Android动态显示当前年月日时分秒系统时间(示例代码)

发布时间:2020-10-07 21:17:01

来源:脚本之家

阅读:113

作者:a15838319826

在布局文件中放一个TextView用来显示时间,如下所示:

xmlns:android=”http://schemas.android.com/apk/res/android”

android:layout_width=”match_parent”

android:layout_height=”match_parent”

android:background=”@android:color/white”>

android:id=”@+id/mytime”

android:layout_width=”match_parent”

android:layout_height=”match_parent”

android:gravity=”center”

android:textColor=”@android:color/black”

android:textSize=”36sp”/>

开启一个线程,然后通过handler发消息,来实时的更新TextView上显示的系统时间:

import android.app.Activity;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.text.format.DateFormat;

import android.widget.TextView;

public class TestActivity extends Activity {

private static final int msgKey1 = 1;

private TextView mTime;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.time);

mTime = (TextView) findViewById(R.id.mytime);

new TimeThread().start();

}

public class TimeThread extends Thread {

@Override

public void run () {

do {

try {

Thread.sleep(1000);

Message msg = new Message();

msg.what = msgKey1;

mHandler.sendMessage(msg);

}

catch (InterruptedException e) {

e.printStackTrace();

}

} while(true);

}

}

private Handler mHandler = new Handler() {

@Override

public void handleMessage (Message msg) {

super.handleMessage(msg);

switch (msg.what) {

case msgKey1:

mTime.setText(getTime());

break;

default:

break;

}

}

};

//获得当前年月日时分秒星期

public String getTime(){

final Calendar c = Calendar.getInstance();

c.setTimeZone(TimeZone.getTimeZone(“GMT+8:00”));

String mYear = String.valueOf(c.get(Calendar.YEAR)); // 获取当前年份

String mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份

String mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码

String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));

String mHour = String.valueOf(c.get(Calendar.HOUR_OF_DAY));//时

String mMinute = String.valueOf(c.get(Calendar.MINUTE));//分

String mSecond = String.valueOf(c.get(Calendar.SECOND));//秒

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 =”六”;

}

return mYear + “年” + mMonth + “月” + mDay+”日”+” “+”星期”+mWay+” “+mHour+”:”+mMinute+”:”+mSecond;

}

}

以上所述是小编给大家介绍的Android动态显示当前年月日时分秒系统时间,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的

struts2中用ajax标签出错或时间控件显示不出来

学习使用struts2的ajax时,看书说要引入<s:head theme=”ajax”/>,但引入后就一直报错

在jsp页面的<head></head>之间插入<s:head theme=”ajax”/>这句话,打开页面弹个对话框提示站点无法访问,如果把<s:head theme=”ajax”/>这句取消了,才能打开页面,但这页面也失去ajax功能了

这主要是strut2版本的问题,不是你代码的问题

页面部分关键代码粘贴在下面了。。

<html>

<head>

<base href=”<%=basePath%>”>

<title>My JSP ‘index.jsp’ starting page</title>

<meta http-equiv=”pragma” content=”no-cache”>

<meta http-equiv=”cache-control” content=”no-cache”>

<meta http-equiv=”expires” content=”0″>

<meta http-equiv=”keywords” content=”keyword1,keyword2,keyword3″>

<meta http-equiv=”description” content=”This is my page”>

<!–

<link rel=”stylesheet” type=”text/css” href=”styles.css”>

–>

<s:head theme=”ajax”/> <!–加了这句就打不开页面–>

</head>

<body>

<s:form action=”register” theme=”ajax” validate=”true”>

<s:textfield label=”用户名” name=”username”></s:textfield>

<s:password label=”密码” name=”password”></s:password>

<s:password label=”重新输入密码” name=”repassword”></s:password>

<s:textfield label=”年龄” name=”age”></s:textfield>

<s:datetimepicker label=”生日” name=”birthday”></s:datetimepicker>加了这句后,时间控件同样显示不出来

<s:submit value=”发布”></s:submit>

</s:form>

<s:property value=”result”/>

</body>

</html>

如果你的Struts版本是2.1.6或以上的,那就要加struts2-dojo-plugin-2.1.8.jar,Struts2升级以后把Ajax和一些控件功能单独提取出来了,且放在了struts2-dojo-plugin-2.1.8.jar里。像我用的就是struts2.1.8,所以要在项目lib中加入struts2-dojo-plugin-2.1.8.jar这个包后, 页面应该改成: <%@ taglib prefix=”s” uri=”/struts-tags”%> <%@ taglib prefix=”sd” uri=”/struts-dojo-tags”%> <head> <s:head theme=”xhtml”/> <sd:parseContent=”true”/></head>….. <sd:datetimepicker label=”生日” name=”birthday”></sd:datetimepicker>而时间空间应该使用sd标签

原文地址: http://download.csdn.net/detail/zzb13425138525/3206869