안드로이드 with 자바/응용 문제들
프로그래스바를 이용하여 진행도 알려주기
백_곰
2022. 5. 18. 16:52
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>