2

我想在我的应用程序屏幕上的标签中显示当前时间(小时:分钟)。

是否可以随着时间的变化自动更新?我的意思是,当分钟(和/或小时)更改时,我希望文本/标签自动更改它的值,而我不必总是检查当前时间并更新它。

有谁知道这是否可能?就像Android API中有一个特殊的“时间”对象连续显示它一样?

谢谢。

编辑:如果我想要的东西是不可能的,你能建议我一个精心设计的手动选择吗?

4

5 回答 5

1

使用 Luksprog 的想法,我创建了一个文本视图,它是自我更新的时钟文本视图。

   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 中。它会自动工作

于 2013-10-03T14:22:55.453 回答
1

有谁知道这是否可能?就像Android API中有一个特殊的“时间”对象连续显示它一样?

没有内置的东西,就像其他人说你会使用 aHandler并自己更新时间一样。看下面一个小例子:

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

于 2013-07-06T12:02:23.797 回答
0

如果你想手动实现,你可以使用 handler 和 countDownTimer 。处理程序将更新文本视图,CountDownTimer 将根据特定频率计算时间。

于 2013-07-06T11:37:46.153 回答
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);
    }


    }
于 2013-10-07T10:35:54.123 回答
0

不,虽然有人可能写了一个视图来做到这一点,但这并不难。我会简单地使用一个处理程序并向它发布一条延迟消息,该消息会在 1 分钟后消失。当处理程序处理消息时,让它更新标签。然后让它自己发送另一条消息,再延迟一分钟。这样,只要您在前台并更新您的文本视图,就会每分钟调用一次。

于 2013-07-06T11:31:43.593 回答