2013-08-01 19 views
0

我正在开发一个android应用程序,并且正在使用phonegap。我也使用jquery mobile。现在我想添加一个时钟或当前时间(更新/更改)显示在应用程序标题的左上角。如何在应用程序的标题中显示不断更新的时间

你可以建议我一种方法来实现这一点或使用一些现有的图书馆。我也可以编写这个代码,但是不要重新发明轮子。

+1

@Waza_Be这是一个更密切的问题,属于“显示无最小理解的问题”类别。 – krishgopinath

+0

你是对的.. –

回答

1

如果精度不是很重要,可以使用handler和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; 
    } 
0
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!

相关问题