Monday, May 21, 2012 : Article > Android User : Register | Login
บทความ
Webboard
วีดีโอ
บทความจากเพื่อนๆ Guru
28
ตัวอย่างนี้เป็นเรื่องของการจับเวลาแบบง่าย ๆ  โดยใช้ฟังก์ชัน elapsedRealtime() และเราสามารถกำหนด Format ของเวลา โดยใช้ฟังก์ชัน setFormat()

1.สร้างโปรเจค


2.ใน file main.xml  ( res>layout ) ใส่โค้ดดังต่อไป

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  android:orientation="vertical"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent"

  >

<TextView

  android:layout_width="fill_parent"

  android:layout_height="wrap_content"

  android:text="@string/hello"

  />

<Chronometer

 android:id="@+id/chronometer"

 android:layout_gravity="center_horizontal"

  android:layout_width="fill_parent"

  android:layout_height="wrap_content"

 />

<Button

 android:id="@+id/buttonstart"

  android:layout_width="fill_parent"

  android:layout_height="wrap_content"

  android:text="Start"

 />

<Button

 android:id="@+id/buttonstop"

  android:layout_width="fill_parent"

  android:layout_height="wrap_content"

  android:text="Stop"

 />

<Button

 android:id="@+id/buttonreset"

  android:layout_width="fill_parent"

  android:layout_height="wrap_content"

  android:text="Reset"

 />

</LinearLayout>

 


3. เปิด file AndriodChronometer.java ( src>th.co.bighead.exsample.AndriodChronometer) และใส่โค้ดต่อไปนี้

package th.co.bighead.exsample.AndriodChronometer;

 

import android.app.Activity;

import android.os.Bundle;

import android.os.SystemClock;

import android.view.View;

import android.widget.Button;

import android.widget.Chronometer;

 

public class AndriodChronometer extends Activity {

   /** Called when the activity is first created. */

   @Override

   public void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.main);

     

       final Chronometer myChronometer = (Chronometer)findViewById(R.id.chronometer);

       Button buttonStart = (Button)findViewById(R.id.buttonstart);

       Button buttonStop = (Button)findViewById(R.id.buttonstop);

       Button buttonReset = (Button)findViewById(R.id.buttonreset);

     

       buttonStart.setOnClickListener(new Button.OnClickListener(){

 

   @Override

   public void onClick(View v) {

    // TODO Auto-generated method stub

    myChronometer.start();

   }});

     

       buttonStop.setOnClickListener(new Button.OnClickListener(){

 

   @Override

   public void onClick(View v) {

    // TODO Auto-generated method stub

    myChronometer.stop();

   

   }});

     

       buttonReset.setOnClickListener(new Button.OnClickListener(){

 

   @Override

   public void onClick(View v) {

    // TODO Auto-generated method stub

    myChronometer.setBase(SystemClock.elapsedRealtime());

 

   }});

     

   }

}


4.รันโปรแกรม

Post Rating

Comments

There are currently no comments, be the first to post one.

Post Comment

Only registered users may post comments.
Home | Article | Webboard | Video | Blog | Showcase | News
Copyright 2010 by devguru.mobi