我想在我的应用程序屏幕上的标签中显示当前时间(小时:分钟)。
是否可以随着时间的变化自动更新? 我的意思是,当分钟(和/或小时)改变时,我希望文本/标签自动更改它的值,而不必总是检查当前时间并更新它。
有人知道这是否可行?就像Android API中有一个特殊的“时间”对象一样连续显示它?
感谢。
编辑:如果我想要的是不可能的,你能否建议我一个精心设计的手动选择?
答案 0 :(得分:1)
有人知道这是否可行?就好像有一个特殊的“时间” 来自Android API的对象连续显示它?
没有内置的东西,就像其他人说你会使用Handler
并自己更新时间。请参阅下面的一个小例子:
private Handler mHandler = new Handler();
private TextView mText;// the TextView
private int mHour, mMinute; // variables holding the hour and minute
private Runnable mUpdate = new Runnable() {
@Override
public void run() {
mMinute += 1;
// just some checks to keep everything in order
if (mMinute >= 60) {
mMinute = 0;
mHour += 1;
}
if (mHour >= 24) {
mHour = 0;
}
// or call your method
mText.setText(mHour + ":" + mMinute);
mHandler.postDelayed(this, 60000);
}
};
@Override
protected void onResume() {
super.onResume();
// whenever the activity is built or resumed update the time and start posting updates
Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
mText.setText(mHour + ":" + mMinute);
mHandler.postDelayed(mUpdate, 60000); // 60000 a minute
}
@Override
protected void onStop() {
super.onStop();
mHandler.removeCallbacks(mUpdate);// we need to remove our updates if the activity isn't focused(or even destroyed) or we could get in trouble
}
而不是setText()
中的Runnable
部分,您可以调用您的方法。
答案 1 :(得分:1)
使用Luksprog的想法,我创建了一个文本视图,它是自我更新的Clock Text视图。
import java.util.Calendar;
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.TextView;
public class TimerTextView extends TextView {
private Handler mHandler = new Handler();
private int mHour, mMinute, mSecond;// variables holding the hour and minute
private Runnable mUpdate = new Runnable() {
@Override
public void run() {
mSecond += 1;
// just some checks to keep everything in order
if (mSecond >= 60) {
mSecond = 0;
mMinute+=1;
}
if (mMinute >= 60) {
mMinute = 0;
mHour += 1;
}
if (mHour >= 24) {
mHour = 0;
}
// or call your method
setText(mHour + ":" + mMinute+":"+mSecond);
mHandler.postDelayed(this, 1000);
}
};
public TimerTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context context) {
mHandler.removeCallbacks(mUpdate);
Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
mSecond = c.get(Calendar.SECOND);
setText(mHour + ":" + mMinute+":"+mSecond);
mHandler.postDelayed(mUpdate, 1000); // 60000 a minute
}
public TimerTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public TimerTextView(Context context) {
super(context);
init(context);
}
}
只需将此自定义Textview放入布局XML中即可。它会自动工作
答案 2 :(得分:0)
不,虽然有人可能写了一个视图,但这并不是很难。我会简单地使用一个处理程序并向它发送延迟消息,该消息在1分钟后消失。当处理程序处理消息时,让它更新标签。然后让它自己发送另一条消息,再延迟一分钟。这样,只要您在前台并更新文本视图,就会每分钟调用一次。
答案 3 :(得分:0)
如果您想要手动实现,可以使用handler和countDownTimer。 处理程序将更新textviews,CountDownTimer将根据特定频率计算时间。
答案 4 :(得分:0)
试试这段代码。 。
public class MainActivity extends Activity {
TextView t;
private Handler mHandler = new Handler();
private int mHour, mMinute,mSecond; // variables holding the hour and minute
String min,hr,sec;
private Runnable mUpdate = new Runnable() {
@Override
public void run() {
Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
mSecond = c.get(Calendar.SECOND);
min = "" +mMinute;
hr = "" +mHour;
sec = "" +mSecond;
if(min.length()==1 ){
min = "0"+mMinute;
}if(hr.length()==1){
hr = "0" +mHour;
}if(sec.length()==1){
sec = "0" +mSecond;
}
t.setText(hr + ":" + min + ":" + sec);
mHandler.postDelayed(this, 1000);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t = (TextView)findViewById(R.id.textView1);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
mSecond = c.get(Calendar.SECOND);
t.setText(mHour + ":" + mMinute + ":" + mSecond);
mHandler.postDelayed(mUpdate, 1000);
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
mHandler.removeCallbacks(mUpdate);
}
}