【问题标题】:Updating TextView with clock用时钟更新 TextView
【发布时间】:2013-04-22 18:20:05
【问题描述】:

我一直在尝试创建一个程序,该程序将输出一个可以工作的数字时钟,让我可以快速访问日期和时间。我有解析时间的代码,但是,我在更新 textview 时遇到了困难。我有这个:

    `public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    timer = (TextView)findViewById(R.id.timer);

    time = new Time();
    time.setToNow();

    timeString = time.toString();
    changeTime = Parser(timeString);


    time.setToNow();
    timeString = time.toString();
    changeTime = Parser(timeString);

    timer.setText(changeTime);  
    } 
    private String Parser(String time){

    String year = time.substring(0, 4);
    String month = time.substring(4,6);
    String day = time.substring(6, 8);
    String hour = time.substring(9,11);
    String minute = time.substring(11, 13);
    String second = time.substring(13, 15);

    String finalTime = hour + ":" + minute + ":" + second + " " + day + " " + month + " "  + year;
    //String finalTime = second;
    return finalTime;


}`

如何将其放入循环中以不断更新文本视图。

感谢您能给我的任何帮助。

【问题讨论】:

  • 您可以使用Thread,TimerTaskHandler.postdelayed 不断更新TextView,而不是使用循环

标签: java android timer textview clock


【解决方案1】:

使用永无止境的处理程序消息开始更新视图是一种不好的做法(尝试查看进程的 CPU 级别)。 更好更优雅的方法是注册一个BroadcastReceiver,它会触发更新。

public class Clock extends LinearLayout {

    private Calendar mCalendar;
    private LinearLayout mLayoutTime;
    private TextView mAMPMText;
    private TextView mDateText;
    private TextView mTimeText;
    private View mSendFeedback;
    private boolean mAttached;

    private final Handler mHandler = new Handler();
    SimpleDateFormat dateFormatter = new SimpleDateFormat("EEE, MMM d, yyyy");
    SimpleDateFormat timeFormatter = new SimpleDateFormat("h:mm");

    public Clock(final Context context, int layoutResourceID, int dateResId, int meResId,int amPmResId) {
        super(context);
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View view = layoutInflater.inflate(layoutResourceID, null);
        addView(view, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        mAMPMText = (TextView) view.findViewById(amPmResId);
        mTimeText = (TextView) view.findViewById(timeResId);
        mDateText = (TextView) view.findViewById(dateResId);    
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();

        if (!mAttached) {
            mAttached = true;
            IntentFilter filter = new IntentFilter();

            filter.addAction(Intent.ACTION_TIME_TICK);
            filter.addAction(Intent.ACTION_TIME_CHANGED);
            filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);

            getContext().registerReceiver(mIntentReceiver, filter, null, mHandler);
        }

        // NOTE: It's safe to do these after registering the receiver since the receiver always runs
        // in the main thread, therefore the receiver can't run before this method returns.

        // The time zone may have changed while the receiver wasn't registered, so update the Time
        mCalendar = Calendar.getInstance();

        // Make sure we update to the current time
        onTimeChanged();
        updateView();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mAttached) {
            getContext().unregisterReceiver(mIntentReceiver);
            mAttached = false;
        }
    }

    private void updateView(){
        mTimeText.setText(timeFormatter.format(mCalendar.getTime()));
        mDateText.setText(dateFormatter.format(mCalendar.getTime()));
        mAMPMText.setText(mCalendar.get(Calendar.AM_PM) == 0 ? "AM" : "PM");
    }

    private void onTimeChanged() {
        mCalendar.setTime(new Date());

        updateContentDescription(mCalendar);
    }

    private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_TIMEZONE_CHANGED)) {
                String tz = intent.getStringExtra("time-zone");
                mCalendar.setTimeZone(TimeZone.getTimeZone(tz));
            }

            onTimeChanged();

            updateView();
        }
    };

    private void updateContentDescription(Calendar calendar) {
        setContentDescription(calendar.toString());
    }
}

布局(可以做得更好)

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/layout_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/time_txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:includeFontPadding="true" />

        <TextView
            android:id="@+id/am_pm_txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="AM" />
    </LinearLayout>

    <TextView
        android:id="@+id/date_txt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

【讨论】:

    【解决方案2】:

    您应该使用计时器线程。您还可以使用Calendar 简化您的时间和日期转换,并可以使用format 优化您的字符串创建。此示例将每秒(1000 毫秒)更新您的 TextView

    Timer t = new Timer();
    timer = (TextView)findViewById(R.id.timer);
    
    t.scheduleAtFixedRate(new TimerTask() 
    {
        @Override
        public void run() {
            Calendar c = Calendar.getInstance();
            final String finalTime = String.format(Locale.US, "%d:%d:%d %d/%d/%d", c.get(Calendar.HOUR), c.get(Calendar.MINUTE), c.get(Calendar.SECOND), c.get(Calendar.DAY_OF_MONTH), c.get(Calendar.MONTH), c.get(Calendar.YEAR));
    
            runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        timer.setText(finalTime);
                    }
                });
        }
    
    }, 1000, 1000); //Initial Delay and Period for update (in milliseconds)
    

    【讨论】:

    • 这似乎使格式化的时间出现,但是,它似乎没有更新。关于我可能做错了什么的任何想法?
    • @JJJ1106,是的 - 我修复了我的帖子。您需要在TimerTask 中创建Calendar 实例。如果这对您很有效,请考虑接受它作为答案,因为它很简单。
    【解决方案3】:

    声明一个处理程序来更新 UI 线程上的 TextView。

    private Handler mHandler = new Handler() {
     @Override
     public void handleMessage(Message msg) {
        time = new Time();
        time.setToNow();
    
        timeString = time.toString();
        changeTime = Parser(timeString); 
        timer.setText(changeTime);  
     }
    };
    

    启动一个 TimeTask 来更新你的 TextView

    int initialDelay = 1000; //first update in miliseconds
    int period = 5000;      //nexts updates in miliseconds       
    
    Timer timer = new Timer();
    TimerTask task = new TimerTask() {
      public void run() {
          Message msg = new Message();
          mHandler.sendMessage(msg);
      }
    };
    timer.scheduleAtFixedRate(task, initialDelay, period);
    

    【讨论】:

      【解决方案4】:

      这可以借助 android 中的线程轻松完成,但由于您想访问 ui,因此线程无法帮助您。你最好选择 handlers 或 timertask

      但是异步任务也在非 ui 线程上运行,所以在 timertask 中创建一个处理程序..

      Timer t=Timer();
      
          t.scheduleAtFixedRate(new TimerTask(new Runnable()
              {
          public void run(){
          Handler h=new Handler(getMainLooper());
          h.post(new Runnable(
      ){public void run(){
      //get the time here and set the textview here as here it has access to the main ui
      }});
          }
          }
          ), long delay, Long period);
      

      抱歉,代码格式不正确......但它会为您工作

      【讨论】:

        【解决方案5】:

        如果操作不耗时,建议使用Handler,而不是TimerThreadHandler 在当前线程中工作,不会创建新线程,这样可以节省一些内存使用。它还通过减少 CPU 轮询线程的数量来节省一些 CPU 使用率。此外,Android 的线程Looper 和时钟SysClock 进行了优化,等待时几乎不消耗资源。

        Handler handler = new Handler(Looper.getMainLooper()) // using the main thread to update view
        Runnable runnable = new Runnable() {
            public void run(){
                tick()
            }
        }
        void tick(){
            Date date = Date()
            String text = date.toString() // or using SimpleDateFormat
            textView.setText(text)
            handler.postDelayed(runnable, 1000)
        }
        

        记得手动结束。

        void onDestroy() {
            super.onDestroy()
            handler.removeCallbacks(runnable)
        }
        

        【讨论】:

          猜你喜欢
          • 2011-07-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-05-12
          • 2015-12-29
          • 2012-07-24
          • 2012-11-01
          相关资源
          最近更新 更多