时钟和动态布局

时间:2015-03-16 20:10:47

标签: java android layout timer

我如何在Textview中连续显示小时分钟和秒钟,因为我使用的是动态线性布局。这种格式适用于静态布局但不适用于动态布局请帮助我谢谢?

  • 列出项目

    public class ButtonActivity extends Activity {
    
    Button b;
    ScrollView scrollview;    
    int count = 4;`enter code here`
    private int mHour,mMinute,mSecond; 
    int min,hr,sec;
    private Handler mHandler = new Handler();
    
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_button);     
      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;
          String timeSet = "";
    
          if (hr > 12) {
            hr -= 12;
            timeSet = "PM";
          } else if (hr == 0) {
            hr += 12;
            timeSet = "AM";
          } else if (hr == 12)
            timeSet = "PM";
            else
            timeSet = "AM";          
            String minutes = "";
            if (min < 10)
            minutes = "0" + min;
            else
            minutes=String.valueOf(min);                
            String secs = "";
            if (sec < 10)
            secs = "0" + sec;
            else
            secs = String.valueOf(sec);
    
    
            mHandler.postDelayed(this, 1000);
    
            }
         };
    
           mHandler.post(mUpdate);            
           scrollview = new ScrollView(this);
           LinearLayout linearlayout = new LinearLayout(this);
         linearlayout.setOrientation(LinearLayout.VERTICAL);             
         scrollview.addView(linearlayout);          
         TextView t = new TextView(this); 
    
       t.setText(hr + ":" + minutes + ":" + secs+""+timeSet);           
      linearlayout.addView(t);       
      for(int i = 0; i<count;i++)
      {
       LinearLayout linear1 = new LinearLayout(this);
          linear1.setOrientation(LinearLayout.HORIZONTAL);
          linearlayout.addView(linear1);              
          b = new Button(this);
          b.setText(""+i);
          b.setId(i);
          b.setTextSize(10);
          b.setPadding(8, 3, 8, 3);
          b.setTypeface(Typeface.SERIF,Typeface.BOLD_ITALIC);
          b.setLayoutParams(newLayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));            
          linear1.addView(b);                            
      }                
      this.setContentView(scrollview);  
    

    }
      }

1 个答案:

答案 0 :(得分:0)

你必须在TextView 里面调用setText()你的Runnable run()方法。现在的方式是文本只在创建活动时设置一次。