안드로이드

 

 

class ProgressActivity
package com.example.choi.ex_06;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class ProgressActivity extends AppCompatActivity implements Runnable {

    //1.변수 선언
    ProgressBar pb;
    TextView txtRate;
    Button btnStart;

    int value; //진행률값


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

        //2.객체 생성
        pb=(ProgressBar)findViewById(R.id.pb);
        txtRate=(TextView)findViewById(R.id.txtRate);
        btnStart=(Button)findViewById(R.id.btnStart);
        //3. 버튼 클릭 이벤트
        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //백그라운드 스레드 생성
                //new Thread(스레드를 구현한 객체)
                Thread th =new Thread(ProgressActivity.this);
                th.start();//스레드 객체의 run()가 호출됨
            }
        });

    }


    //Alt+Insert , Implement Methods
    //백그라운드 스레드에서 실행하는 부분
    //백그라운드 스레드에서는 메인 UI를 직접 터치할 수 없다.
    @Override
    public void run() {

        for(int i=0; i<=100; i++){
            //진행률 값 변경
            value=i;
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    pb.setProgress(value);
                    txtRate.setText("진행률 : " +value +"%");
                }
            });

            try{
                Thread.sleep(100);
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(ProgressActivity.this, "작업이 완료되었습니다.",
                        Toast.LENGTH_SHORT).show();
            }
        });

    }




}



 

progress.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/progress"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    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="com.example.choi.ex_06.ProgressActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/pb"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="65dp"
        android:id="@+id/txtRate" />

    <Button
        android:text="시작"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtRate"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="68dp"
        android:id="@+id/btnStart" />

    <ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/pb"
        android:max="100"
        android:progress="0"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

 

 

 

 

 

about author

PHRASE

Level 60  머나먼나라

배우는 자가 도저히 자기가 배울 수 없는 것을 배우려 한다. 쓸데없는 정력을 소모하고 있는 것이다. -장자

댓글 ( 4)

댓글 남기기

작성