* Thread_1
* activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:max="100"
android:progress="20"></ProgressBar>
<Button
android:id="@+id/btnInc"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="10씩 증가"></Button>
<Button
android:id="@+id/btnDec"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="10씩 감소"></Button>
<TextView
android:id="@+id/tvSeek"
android:layout_width="match_parent"
android:layout_height="wrap_content"></TextView>
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="match_parent"></SeekBar>
</LinearLayout>
</LinearLayout>
* 화면
* MainActivity.java
package com.example.thread_1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ProgressBar pb1;
Button btnInc, btnDec;
pb1 = (ProgressBar) findViewById(R.id.progressBar1);
btnInc = (Button) findViewById(R.id.btnInc);
btnDec = (Button) findViewById(R.id.btnDec);
btnInc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pb1.incrementProgressBy(10);
}
});
btnDec.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pb1.incrementProgressBy(-10);
}
});
final TextView tvSeek = (TextView) findViewById(R.id.tvSeek);
SeekBar seekBar1 = (SeekBar) findViewById(R.id.seekBar1);
seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tvSeek.setText("진행률 : " + progress + " %");
}
});
}
}
* 실행 화면
* Thread_2
* Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<SeekBar
android:id="@+id/pb1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:max="100"
android:progress="10"></SeekBar>
<SeekBar
android:id="@+id/pb2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:max="100"
android:progress="30"></SeekBar>
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="스레드 시작"></Button>
</LinearLayout>
</LinearLayout>
* 화면
* MainAcrivity.java
package com.example.thread_2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
ProgressBar pb1, pb2;
Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pb1 = (ProgressBar) findViewById(R.id.pb1);
pb2 = (ProgressBar) findViewById(R.id.pb2);
btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Thread(){
public void run() {
for (int i = pb1.getProgress(); i < 100; i += 2)
{
pb1.setProgress(pb1.getProgress()+2);
SystemClock.sleep(100);
}
}
}.start();
new Thread(){
public void run(){
for (int i = pb2.getProgress(); i < 100; i++)
{
pb2.setProgress(pb2.getProgress()+1);
SystemClock.sleep(100);
}
}
}.start();
}
});
}
}
* 실행화면
* Thread_3
* activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="1번 진행율 : "></TextView>
<SeekBar
android:id="@+id/pb1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:max="100"
android:progress="10"></SeekBar>
<TextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="2번 진행율 : "></TextView>
<SeekBar
android:id="@+id/pb2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:max="100"
android:progress="20"></SeekBar>
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="스레드 시작"></Button>
</LinearLayout>
</LinearLayout>
* MainActivity.java
package com.example.thread_3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView tv1, tv2;
ProgressBar pb1, pb2;
Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.tv1);
tv2 = (TextView) findViewById(R.id.tv2);
pb1 = (ProgressBar) findViewById(R.id.pb1);
pb2 = (ProgressBar) findViewById(R.id.pb2);
btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Thread(){
public void run(){
for (int i = pb1.getProgress(); i < 100; i += 2)
{
runOnUiThread(new Runnable() {
@Override
public void run() {
pb1.setProgress(pb1.getProgress() + 2);
tv1.setText("1번 진행률 : " + pb1.getProgress() + " %");
}
});
SystemClock.sleep(100);
}
}
}.start();
new Thread(){
public void run(){
for (int i = pb2.getProgress(); i < 100; i++)
{
runOnUiThread(new Runnable() {
@Override
public void run() {
pb2.setProgress(pb2.getProgress()+1);
tv2.setText("2번 진행률 : " + pb2.getProgress() + " %");
}
});
SystemClock.sleep(100);
}
}
}.start();
}
});
}
}
* 실행 화면
'Android Studio' 카테고리의 다른 글
Android Studio - ListView (0) | 2021.03.08 |
---|---|
Android Studio - Content Provider(통화기록 가져오기) (0) | 2021.03.08 |
Android Studio - Broadcast Receiver (0) | 2021.03.08 |
Android Studio - SQLite_1 (가수 그룹 관리 DB 만들기) (2) | 2021.03.08 |
Android Studio - Logcat (0) | 2021.03.08 |