初学Android做计时器和代码

今天做的第一个程序是Head First Android development的Chapter4做一个计时app .

Android 开发分为layout和activity开发,当然这样说肯定不合理,就先这么理解,为了继续学习。简化问题是一个不错的学习方式,但是会造成眼高手低的毛病。下面是layout开发
 
<? xml version= "1.0" encoding= "utf-8" ?>
< LinearLayout<!--线性布局方式-->
        xmlns: android = "http://schemas.android.com/apk/res/android"            xmlns: app = "http://schemas.android.com/apk/res-auto"  
        xmlns: tools = "http://schemas.android.com/tools"  
        android :layout_width= "match_parent"  
        android :layout_height= "match_parent"  
        android :paddingBottom= "16dp"  
        android :paddingLeft= "16dp"  
        android :paddingRight= "16dp"
        android :paddingTop= "16dp"  
        tools:context="com.example.a17315.stopwatch.StopwatchActivity" 
        android:orientation="vertical"<!--子控件TextView,Button垂直排列--> 
        android:gravity="center_horizontal"<!--内部元素TextView,Button居中方式-->>
    < TextView  
        android :id= "@+id/time_view"  
         android :layout_width= "wrap_content"  
        android :layout_height= "wrap_content"  
        android :layout_alignParentTop= "true"  
        android :layout_centerHorizontal= "true"  
        android :layout_marginTop= "0dp"  
        android :text= ""  
        android :textAppearance= "?android:attr/textAppearanceLarge"  
        android :textSize= "92sp" />
< Button  
       android :id= "@+id/start_button"  
       android :layout_width= "wrap_content"  
       android :layout_height= "wrap_content"  
       android :layout_below= "@+id/time_view"  
       android :layout_marginTop= "20dp"  
       android :layout_centerHorizontal= "true"  
       android :onClick= "onClickStart"<!--Android 的 activity里面要实现的方法-->  
       android :text= "@string/start" /><!--在res文件夹下的values下的string.xml中需要添加的值如下图-->
< Button  
       android :id= "@+id/stop_button"  
       android :layout_width= "wrap_content"  
       android :layout_height= "wrap_content"  
       android :layout_below= "@+id/start_button"  
       android :layout_marginTop= "10dp"  
       android :layout_centerHorizontal= "true"  
       android :onClick= "onClickStop"  
       android :text= "@string/stop" />
< Button  
       android :id= "@+id/rest_button"  
       android :layout_width= "wrap_content"  
       android :layout_height= "wrap_content"  
       android :layout_below= "@id/stop_button"  
       android :layout_marginTop= "10dp"  
       android :layout_centerHorizontal= "true"  
       android :onClick= "onClickReset"  
       android :text= "@string/reset" /></ LinearLayout>
String.xml中的内容
                        
下面就是StopwatchActivity.java

 
        
package com.example.a17315.stopwatch;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.TextView;
public class StopwatchActivity extends AppCompatActivity {
//Numbers of seconds displayed in the stopwatch  
   private int seconds = 0;
//Is the stopwatch running  
   private boolean running;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout. activity_stopwatch);
runTimer();
}
//Start the stopwatch running when the Start button is clicked  
   public void onClickStart(View view){ running = true;
}
//Stop the stopwatch running when the Stop button is clicked  
   public void onClickStop(View view){
running = false;
}
/ /Reset the stopwatch running when the Reset button is clicked  
   public void onClickReset(View view){
running = false; seconds = 0;
}
private void runTimer(){
final TextView timeView = (TextView)findViewById(R.id. time_view);
final Handler handler = new Handler(); handler.post( new Runnable() {
@Override  
         public void run() {
int hours = seconds/ 3600;
int minutes = ( seconds% 3600)/ 60;
int secs = seconds% 60; String time = String. format( "%d:%02d:%02d",hours,minutes,secs);
t imeView.setText(time);
if( running){
seconds++;
}
handler.postDelayed( this, 1000);
}
});
}
}

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值