프로그래스바를 이용하여 진행도 알려주기

2022. 5. 18. 16:52·안드로이드 with 자바/응용 문제들

0. 응용한 기술

- 해당 섹션이 완료되면, 그 섹션만큼 진행도를 증가시키는 것.

( 여기서는 섹션을 sleep()을 주었다. )

( 구현하고 싶다면, sleep()을 진행시키고 여러 쓰레드를 수행하면 된다. )

 

 

 

 

1. 구현해야 할 것

 

 

 

2. 제약조건

- 없음.

 

 

 

3. java 소스코드

 

( 아래는 MainActivity.java의 코드이다. )

package com.example.exercise001;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements Runnable{
    static ProgressDialog dialog;
    static ProgressBar progressBar;
    static Context mContext;

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

        progressBar = findViewById(R.id.progressBar);
        progressBar.setIndeterminate(false);
        mContext = getApplicationContext();

        Button btn1 = findViewById(R.id.btn1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog = new ProgressDialog(MainActivity.this);
                dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                dialog.setMessage("메시지를 전송중입니다.");
                dialog.setTitle("알림");
                progressBar.incrementProgressBy(30);
                dialog.show();

                try{
                    //ThreadEX1 t1 = new ThreadEX1();
                    //t1.start();

                    MainActivity m = new MainActivity();
                    Thread t1 = new Thread(m);
                    t1.start();
                }catch (Exception e) {

                }finally {
                    printStr("전송 중...");
                }
            }
        });

        Button btn2 = findViewById(R.id.btn2);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressBar.setProgress(0);
            }
        });
    }

    public void printStr(String msg){
        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void run() {
        try {
            if(progressBar.getProgress() == 100){
                progressBar.setProgress(30);
            }
            Thread.sleep(2000);
            progressBar.incrementProgressBy(10);
            Thread.sleep(1000);
            progressBar.incrementProgressBy(15);
            Thread.sleep(300);
            progressBar.incrementProgressBy(15);
            Thread.sleep(300);
            progressBar.incrementProgressBy(20);
            Thread.sleep(1000);
            progressBar.incrementProgressBy(10);
            dialog.dismiss();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            Handler handler = new Handler(Looper.getMainLooper());
            handler.postDelayed(new Runnable() {
                @Override
                public void run()
                {
                    Toast.makeText(mContext, "전송 완료", Toast.LENGTH_SHORT).show();
                }
            }, 0);
        }
    }
}

 

 

 

4. xml 소스코드

 

( 아래는 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"
    tools:context=".MainActivity">


    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="보여주기" />

        <Button
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="리셋" />
    </LinearLayout>
</LinearLayout>

 

 

 

 

저작자표시 (새창열림)

'안드로이드 with 자바 > 응용 문제들' 카테고리의 다른 글

애니메이션을 이용한 카드뷰 만들어보기(1)  (0) 2022.07.26
리싸이클러뷰를 이용하여 정보 추가, 삭제하기 (2)  (0) 2022.07.26
리싸이클러뷰를 이용하여 정보 추가, 삭제하기 (1)  (0) 2022.07.21
api를 이용하여 영화 정보 가져오기  (0) 2022.06.18
'안드로이드 with 자바/응용 문제들' 카테고리의 다른 글
  • 애니메이션을 이용한 카드뷰 만들어보기(1)
  • 리싸이클러뷰를 이용하여 정보 추가, 삭제하기 (2)
  • 리싸이클러뷰를 이용하여 정보 추가, 삭제하기 (1)
  • api를 이용하여 영화 정보 가져오기
백_곰
백_곰
  • 백_곰
    친절한 코딩
    백_곰
  • 전체
    오늘
    어제
    • 분류 전체보기
      • 알고리즘 (with JAVA)
        • 기본 알고리즘
        • 완전 탐색
        • 분할 정복 알고리즘
        • 동적 계획법
        • 탐욕법
        • 코딩 테스트 기출 문제
        • 코드트리 조별과제
      • 백준 (with JAVA)
        • 완전 탐색
        • 분할 정복
        • 그 외
      • 자바
        • 개발 환경 구축하기
        • 팁
        • 기본적인 개념
        • 컬렉션 프레임워크
        • 프로세스와 쓰레드
        • 지네릭스
        • 람다식
        • 스트림
        • 입출력 IO
        • 네트워킹
        • 열거형(enums)
        • java.lang 패키지
        • java.time 패키지
        • 유용한 클래스들
        • 형식화 클래스들
      • 안드로이드 with 자바
        • 응용 문제들
        • 자잘한 문제들
        • 오류 보고서
  • 블로그 메뉴

    • 링크

    • 공지사항

    • 인기 글

    • 태그

      소켓 프로그래밍
      불안정 정렬
      안정 정렬
      제자리 정렬
      InputStream
      스트림
      java.lang패키지
      람다식
      Collections Framework
      ServerSocket
      outputstream
      snail
      코드트리
      문자 기반 스트림
      중간연산
      알고스팟
      map()
      코딩테스트
      안드로이드 스튜디오
      다형성
      코딩트리조별과제
      유용한 클래스
      TCP 소켓 프로그래밍
      선택 정렬
      file
      역직렬화
      Arrays
      java.time 패키지
      serializable
      자바 개념
    • 최근 댓글

    • 최근 글

    • hELLO· Designed By정상우.v4.10.3
    백_곰
    프로그래스바를 이용하여 진행도 알려주기
    상단으로

    티스토리툴바