我正在开发一个android应用程序,并且我正在使用phonegap。我也在使用jquery mobile。现在我想添加一个时钟或当前时间(更新/更改)显示在我的应用程序标题的左上角。
您能否建议我一种方法来实现这一目标或为此使用一些现有的库。我也可以编写代码,但尽量不要重新发明轮子。
我正在开发一个android应用程序,并且我正在使用phonegap。我也在使用jquery mobile。现在我想添加一个时钟或当前时间(更新/更改)显示在我的应用程序标题的左上角。
您能否建议我一种方法来实现这一目标或为此使用一些现有的库。我也可以编写代码,但尽量不要重新发明轮子。
如果准确性不是很重要,您可以使用处理程序和 postDelayed()。
Handler handler = new Handler();
void timer() {
//Update UI with current time then post a new runnable to run after 1000ms
handler.postDelayed(new Runnable() {
@Override
public void run() {
timer();
}
}, 1000);
return;
}
public class Header extends LinearLayout {
private LayoutInflater inflater;
private Activity foot_activity;
protected TextView txtCurrentTime;
public Header(Context context) {
super(context);
}
public Header(Context context, AttributeSet attrs) {
super(context, attrs);
}
public Header(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setActivity(Activity activity) {
foot_activity = activity;
inflateHeader();
}
private void inflateHeader() {
inflater = (LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.foot, this);
Runnable runnable = new CountDownRunner();
Thread myThread = new Thread(runnable);
myThread.start();
}
class CountDownRunner implements Runnable {
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
doWork();
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
public void doWork() {
foot_activity.runOnUiThread(new Runnable() {
public void run() {
try {
txtCurrentTime = (TextView) findViewById(R.id.lbltimes);
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat(
"dd-MM-yyyy HH:mm:ss a");
String formattedDate = df.format(c.getTime());
txtCurrentTime.setText(formattedDate);
//System.out.println("TIME is : " + formattedDate);
} catch (Exception e) {
}
}
});
}
}
要在所需的活动中检索它,只需包含以下片段:
Header obj = (Header) findViewById(R.id.footer);
obj.setActivity(this);
礼貌:POOVIZHI RAJAN!!!