首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何制作简单的Android二进制文本时钟?

如何制作简单的Android二进制文本时钟?
EN

Stack Overflow用户
提问于 2018-08-02 00:40:06
回答 1查看 0关注 0票数 0

我想创建一个简单的Android二进制时钟,以下是我的代码,但运行时崩溃了

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Binary extends Activity implements Runnable
{
 Thread runner;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

  if (runner == null)
  { //start the song
   runner = new Thread(this);
   runner.start();
  }
    }

 @Override
 public void run()
 {
  TextView hours_dec = (TextView) findViewById(R.id.hours_dec);
  TextView mins_dec = (TextView) findViewById(R.id.mins_dec);
  TextView secs_dec = (TextView) findViewById(R.id.secs_dec);
  TextView hours_bin = (TextView) findViewById(R.id.hours_bin);
  TextView mins_bin = (TextView) findViewById(R.id.mins_bin);
  TextView secs_bin = (TextView) findViewById(R.id.secs_bin);

        SimpleDateFormat hours_sdf = new SimpleDateFormat("HH");
        SimpleDateFormat mins_sdf = new SimpleDateFormat("mm");
        SimpleDateFormat secs_sdf = new SimpleDateFormat("ss");

        Calendar cal = Calendar.getInstance();

  while (runner != null)
  {
   WaitAMoment();
   cal.getTime();

   hours_dec.setText(hours_sdf.format(cal.getTime()));
   mins_dec.setText(mins_sdf.format(cal.getTime()));
   secs_dec.setText(secs_sdf.format(cal.getTime()));

   hours_bin.setText(String.valueOf(Integer.toBinaryString(Integer.parseInt((String) hours_dec.getText()))));
   mins_bin.setText(String.valueOf(Integer.toBinaryString(Integer.parseInt((String) mins_dec.getText()))));
   secs_bin.setText(String.valueOf(Integer.toBinaryString(Integer.parseInt((String) secs_dec.getText()))));
  }

 }

 protected void WaitAMoment()
 {
  try
  {
   Thread.sleep(100);
  } catch (InterruptedException e) { };
 }
}`
EN

回答 1

Stack Overflow用户

发布于 2018-08-02 10:19:35

试试以下修改后的代码:

package stackoverflow.test.binaryclock;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.TextView;

public class BinaryClock extends Activity implements Runnable
{
    Date currentTime;
     Thread runner;
     final Runnable updater = new Runnable(){
        public void run(){
            updateClockValues();
        };
     };
     final Handler mHandler = new Handler();
    TextView hours_dec;
    TextView mins_dec;
    TextView secs_dec;
    TextView hours_bin;
    TextView mins_bin;
    TextView secs_bin;

    SimpleDateFormat hours_sdf = new SimpleDateFormat("HH");
    SimpleDateFormat mins_sdf = new SimpleDateFormat("mm");
    SimpleDateFormat secs_sdf = new SimpleDateFormat("ss");

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        currentTime = Calendar.getInstance().getTime();
        hours_dec = (TextView) findViewById(R.id.hours_dec);
        mins_dec = (TextView) findViewById(R.id.mins_dec);
        secs_dec = (TextView) findViewById(R.id.secs_dec);
        hours_bin = (TextView) findViewById(R.id.hours_bin);
        mins_bin = (TextView) findViewById(R.id.mins_bin);
        secs_bin = (TextView) findViewById(R.id.secs_bin);
        if (runner == null)
        { //start the song
           runner = new Thread(this);
           runner.start();
        }
      }

 private void updateClockValues() {
        currentTime = Calendar.getInstance().getTime();
        hours_dec.setText(hours_sdf.format(currentTime.getTime()));
        mins_dec.setText(mins_sdf.format(currentTime.getTime()));
        secs_dec.setText(secs_sdf.format(currentTime.getTime()));

        hours_bin.setText(String.valueOf(Integer.toBinaryString(Integer.parseInt((String) hours_dec.getText()))));
        mins_bin.setText(String.valueOf(Integer.toBinaryString(Integer.parseInt((String) mins_dec.getText()))));
        secs_bin.setText(String.valueOf(Integer.toBinaryString(Integer.parseInt((String) secs_dec.getText()))));
    }
@Override
 public void run()
 {
  while (runner != null)
  {
      try
      {
       Thread.sleep(1000);
       Log.i("Tick", "Tock");
      } catch (InterruptedException e) { };
      mHandler.post(updater);
     }
 }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100000475

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档