마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

버튼을 누르면 프로그래스바 진행하게 하기 질문좀 드리겠습니다..

0 추천
 
안녕하세요. 질문이 있어서 이렇게 글을 올리네요..

이렇게 생겼는데요...

btn1을 누르면 쓰레드로 첫번째 프로그래스바가 진행이 되고
btn2을 누르면 쓰레드로 두번째 프로그래스바가 진행이 되고
btn3을 누르면 쓰레드로 세번째 프로그래바가 진행이 되게 하고 싶은데
 
어떻게 해야 할까요..? 
 
package com.example.administrator.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity  implements Runnable
{
    ProgressBar bar1;
    ProgressBar bar2;
    ProgressBar bar3;

    EditText txt1;
    EditText txt2;
    EditText txt3;

    Button btn1;
    Button btn2;
    Button btn3;

    int progress1=0;
    int progress2=0;
    int progress3=0;

    Thread thread;

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

        bar1 = (ProgressBar)findViewById(R.id.bar1);
        txt1 = (EditText)findViewById(R.id.txt1);
        btn1 = (Button)findViewById(R.id.btn1);
        bar2 = (ProgressBar)findViewById(R.id.bar2);
        txt2 = (EditText)findViewById(R.id.txt2);
        btn2 = (Button)findViewById(R.id.btn2);
        bar3 = (ProgressBar)findViewById(R.id.bar3);
        txt3 = (EditText)findViewById(R.id.txt3);
        btn3 = (Button)findViewById(R.id.btn3);

        btn1.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v)
            {
                Toast.makeText(getApplicationContext(), "1번 버튼", Toast.LENGTH_SHORT).show();
            }
        });

        btn2.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v)
            {
                Toast.makeText(getApplicationContext(), "2번 버튼", Toast.LENGTH_SHORT).show();
            }
        });

        btn3.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v)
            {
                Toast.makeText(getApplicationContext(), "3번 버튼", Toast.LENGTH_SHORT).show();
            }
        });

        txt1.addTextChangedListener(new TextWatcher()
        {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {
                //TODO Auto-generated method stub
                try
                {
                    progress1 = Integer.parseInt(txt1.getText().toString());
                    
                    bar1.setProgress(progress1);
                }
                catch (Exception e){}
            }
            @Override
            public void afterTextChanged(Editable s){}
        });

        txt2.addTextChangedListener(new TextWatcher()
        {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {
                //TODO Auto-generated method stub
                try
                {
                    progress2 = Integer.parseInt(txt2.getText().toString());
                    bar2.setProgress(progress2);
                }
                catch (Exception e){}
            }
            @Override
            public void afterTextChanged(Editable s){}
        });

        txt3.addTextChangedListener(new TextWatcher()
        {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {
                //TODO Auto-generated method stub
                try
                {
                    progress3 = Integer.parseInt(txt3.getText().toString());
                    bar3.setProgress(progress3);
                }
                catch (Exception e){}
            }
            @Override
            public void afterTextChanged(Editable s){}
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    public void Start(View v)
    {
        thread = new Thread(this);
        thread.start();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void run()
    {
        progress1=0;

        while(progress1 < 100)
        {
            ++progress1;

            bar1.setProgress(progress1);

            try
            {
                thread.sleep(1000);
            }
            catch (Exception e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
}

 

 

고양이주인 (240 포인트) 님이 2015년 8월 1일 질문
고양이주인님이 2015년 8월 1일 reshown

1개의 답변

0 추천

쓰레드를 사용해보세요.

http://kcizzang.tistory.com/entry/%EC%95%88%EB%93%9C%EB%A1%9C%EC%9D%B4%EB%93%9C-ProgressBar-%EC%99%80-Thread

이 링크 이외도 여기에서 쓰레드로 검색해서 참고해보시고 적용하시면 될 듯 합니다.

컴러기 (22,230 포인트) 님이 2015년 8월 3일 답변
...