왕현성
코딩발자취
왕현성
전체 방문자
오늘
어제
  • 코딩 (277)
    • Python (71)
    • Java (16)
    • MySQL (34)
    • 인공지능 (48)
      • 머신러닝 (16)
      • 딥러닝 (32)
    • 영상처리 (4)
    • Rest API (21)
    • Android Studio (25)
    • streamlit (13)
    • DevOps (22)
      • AWS (9)
      • PuTTY (5)
      • Git (4)
      • Serverless (2)
      • Docker (2)
    • IT 기술 용어 (6)
    • 디버깅 ( 오류 해결 과정 ) (17)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • matplotlib
  • unsupervised
  • PYTHON
  • OpenCV
  • 의료이미징
  • UnboundLocalError
  • 영상처리
  • encoding='utf-8'
  • alibi-detection
  • pip install labelme
  • labelme
  • imageprocessing
  • tune()
  • 비지도학습
  • ComputerVision
  • pytorch
  • get_long_description
  • PIL
  • 딥러닝
  • maskimage
  • 영상기술
  • yolov8
  • numpy
  • labelme UnocodeDecodeError
  • TensorFlow
  • 컴퓨터비전
  • 영상처리역사
  • alibidetect
  • 기상탐사
  • ckpt_file

최근 댓글

최근 글

티스토리

250x250
hELLO · Designed By 정상우.
왕현성

코딩발자취

Android Studio

Android - CountDownTimer

2023. 1. 27. 17:33
728x90

전체 코드 ( 코드 안에 주석처리로 설명 )

 

 

package com.hyunsungkr.timer;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {

    TextView txtTimer;
    TextView txtCount;
    Button button;

    CountDownTimer timer;

    boolean isFinished = false;

    int count;


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

        txtTimer = findViewById(R.id.txtTimer);
        txtCount = findViewById(R.id.txtCount);
        button = findViewById(R.id.button);


        timer = new CountDownTimer(10000,1000) {
            @Override
            public void onTick(long l) {
                // 위에서 정한 인터벌이 될때마다 실행되는 함수
                // 인터벌마다 무엇인가 실행하고 싶으면, 여기에 코드 작성

                // 위의 L은 밀리 세컨즈이므로, 초로 표시하려면
                // L 을 1000으로 나눠준다.
                long remain = l / 1000;

                // 화면에 남은 시간을 보여준다.
                txtTimer.setText(""+remain + "초");

                Log.i("TimerAPP Main", ""+remain );



            }
            private void showAlertDialog(){
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

                builder.setTitle("타이머 종료!");
                builder.setMessage("다시 도전하시겠습니까?");
                builder.setNegativeButton("Cancel",null);
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        count=0;
                        isFinished=false;
                        txtCount.setText(count+"");
                        timer.start();
                    }
                });

                builder.show();}

            @Override
            public void onFinish() {
                // 타이머가 종료될 때 실행되는 함수.
                // 종료될 때 하고싶은 코드를 여기에 작성.
                Log.i("TimerAPP Main", "타이머 종료");

                isFinished = true;
                showAlertDialog();






            }
        };

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 1. 타이머가 종료되었는지 확인!
                if(isFinished == true){
                    return;
                }

                count = count+1;
                txtCount.setText(""+count);

            }
        });



        timer.start();



    }
}

'Android Studio' 카테고리의 다른 글

Android - Activity 간의 데이터 전달 방법 (단방향/양방향) / Back(뒤로가기) 이벤트 처리 방법  (0) 2023.01.30
Android - Activity Life Cycle 주요 함수와 화면 전환 방법  (0) 2023.01.30
Android - AlertDialog 사용법  (0) 2023.01.27
Android - TextView의 setText함수에 숫자를 쉽게 넣어주는 방법 / todo  (0) 2023.01.26
Android - EditText사용법과 문자열 가져오는 방법 / 로그처리하는 방법 / Toast , Snackbar 메시지 처리방법  (0) 2023.01.26
    'Android Studio' 카테고리의 다른 글
    • Android - Activity 간의 데이터 전달 방법 (단방향/양방향) / Back(뒤로가기) 이벤트 처리 방법
    • Android - Activity Life Cycle 주요 함수와 화면 전환 방법
    • Android - AlertDialog 사용법
    • Android - TextView의 setText함수에 숫자를 쉽게 넣어주는 방법 / todo
    왕현성
    왕현성
    AI 머신비전 학습일지

    티스토리툴바