0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Android Studio ストップウォッチアプリ制作(動画有り)

Last updated at Posted at 2021-03-06

今回はストップウォッチアプリを制作しました。
YouTubeに作っている動画をアップしています。
https://www.youtube.com/watch?v=1c0LvmCW5EU&list=PLhg2PHSq8bjjjBpOPll39ZAUgtbnlDOvU

strings.xml
<resources>
    <string name="app_name">StopWatchApp</string>
    <string name="btn_start">START</string>
    <string name="btn_stop">STOP</string>
    <string name="btn_reset">RESET</string>
    <string name="btn_lap">LAP</string>
</resources>
list_items.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textSize="24sp"></TextView>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="5"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text_timer"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="00:00.000"
        android:gravity="center"
        android:textSize="36sp"></TextView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:gravity="center"
        android:layout_weight="1">

        <Button
            android:id="@+id/btn_start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:onClick="startTimer"
            android:text="@string/btn_start"></Button>

        <Button
            android:id="@+id/btn_stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:onClick="stopTimer"
            android:text="@string/btn_stop"></Button>

        <Button
            android:id="@+id/btn_reset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:onClick="resetTimer"
            android:text="@string/btn_reset"></Button>

    </LinearLayout>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2.5"
        android:id="@+id/list"></ListView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:gravity="center"
        android:layout_weight="0.5">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btn_lap"
            android:id="@+id/btn_lap"
            android:onClick="tapLap"></Button>

    </LinearLayout>

</LinearLayout>
MainActivity.xml
package com.example.stopwatchapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    Handler handler = new Handler();
    Runnable runnable;
    Button btn_start, btn_stop, btn_reset;
    long startTime;
    TextView text_timer;
    long t, elapsedTime;
    ArrayList<String> lapTime;
    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_start = (Button) findViewById(R.id.btn_start);
        btn_stop = (Button) findViewById(R.id.btn_stop);
        btn_reset = (Button) findViewById(R.id.btn_reset);
        text_timer = (TextView) findViewById(R.id.text_timer);
        listView = findViewById(R.id.list);
        lapTime = new ArrayList<String>();

        btnState(true, false, false);
    }

    public void btnState(boolean start, boolean stop, boolean reset) {
        btn_start.setEnabled(start);
        btn_stop.setEnabled(stop);
        btn_reset.setEnabled(reset);
    }

    public void startTimer(View view) {
        startTime = System.currentTimeMillis();
        runnable = new Runnable() {
            @Override
            public void run() {
                t = System.currentTimeMillis() - startTime + elapsedTime;
                SimpleDateFormat sdf = new SimpleDateFormat("mm:ss.SSS", Locale.US);
                text_timer.setText(sdf.format(t));
                handler.removeCallbacks(runnable);
                handler.postDelayed(runnable, 10);
            }
        };
        handler.postDelayed(runnable, 10);
        btnState(false, true, false);
    }

    public void stopTimer(View view) {
        elapsedTime += System.currentTimeMillis() - startTime;
        handler.removeCallbacks(runnable);
        btnState(true, false, true);
    }

    public void resetTimer(View view) {
        elapsedTime = 0;
        t = 0;
        text_timer.setText("00:00.000");
        btnState(true, false, false);
        lapTime.clear();
        listView.setAdapter(new ArrayAdapter<String>(MainActivity.this,R.layout.list_items,lapTime));
    }

    public void tapLap(View view){
        lapTime.add(text_timer.getText().toString());
        ArrayAdapter<String> adapter =
                new ArrayAdapter<String>(MainActivity.this,R.layout.list_items,lapTime);
        listView.setAdapter(adapter);
    }
}

YouTubeに作っている動画をアップしています。
https://www.youtube.com/watch?v=1c0LvmCW5EU&list=PLhg2PHSq8bjjjBpOPll39ZAUgtbnlDOvU
以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?