본문 바로가기

Android

날짜와 시간 - Chronometer

Activity_main.xml

 

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

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/LinearLayout1"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >

 

    <Chronometer

        android:id="@+id/chrono"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:format="경과시간=%s"

        android:textSize="30sp" />

 

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >

 

        <Button

            android:id="@+id/btn1"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="start"/>

 

        <Button

            android:id="@+id/btn2"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="stop"/>

 

        <Button

            android:id="@+id/btn3"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="reset"/>

    </LinearLayout>

 

</LinearLayout>

 

MainActivity.java

 

package com.example.chronometerex01;

 

import android.os.Bundle;

 

import android.os.SystemClock;

import android.app.Activity;

import android.view.Menu;

import android.view.View;

import android.widget.Chronometer;

 

public class MainActivity extends Activity {

     Chronometer chronometer;

 

     @Override

     protected void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);

           setContentView(R.layout.activity_main);

 

           chronometer = (Chronometer) findViewById(R.id.chrono);

          

           findViewById(R.id.btn1).setOnClickListener(clickListener);

           findViewById(R.id.btn2).setOnClickListener(clickListener);

           findViewById(R.id.btn3).setOnClickListener(clickListener);

     }

 

     private View.OnClickListener clickListener = new View.OnClickListener() {

 

           @Override

           public void onClick(View v) {

                // TODO Auto-generated method stub

                if (v.getId() == R.id.btn1) {

                     chronometer.start();

                } else if (v.getId() == R.id.btn2) {

                     chronometer.stop();

                } else if (v.getId() == R.id.btn3) {

                     chronometer.setBase(SystemClock.elapsedRealtime());

                }

           }

     };

 

     @Override

     public boolean onCreateOptionsMenu(Menu menu) {

           // Inflate the menu; this adds items to the action bar if it is present.

           getMenuInflater().inflate(R.menu.main, menu);

           return true;

     }

 

}








 

'Android' 카테고리의 다른 글

기타위젯 - SlidingDrawer  (0) 2013.04.02
기타위젯 - AutoCompleteTextView  (0) 2013.04.02
날짜와 시간 - DatePickerDialog,TimePickerDialog  (0) 2013.04.02
날짜와 시간 - DatePicker  (0) 2013.04.02
래이팅바(RatingBar)  (0) 2013.04.02