* MainActivity.java
public class MainActivity extends AppCompatActivity {
TextView textView;
Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BackgroundThread thread = new BackgroundThread();
thread.start();
}
});
}
class BackgroundThread extends Thread {
int value = 0;
public void run() {
for (int i = 0; i < 100; i++) {
try {
Thread.sleep(1000);
} catch (Exception e) {
//
}
value++;
Log.d("MyThread", "value : " + value);
handler.post(new Runnable() {
@Override
public void run() {
textView.setText("값 : " + value);
}
});
handler.postDelayed(new Runnable() {
@Override
public void run() {
textView.setText("값 : " + value);
}
}, 5000);
}
}
}
}
'android' 카테고리의 다른 글
Socket 통신(Thread 필수) (0) | 2020.02.04 |
---|---|
AsyncTask 사용법 (0) | 2020.02.04 |
키패드 닫기 (0) | 2020.02.03 |
SeekBar 사용법 (0) | 2020.02.03 |
입력된 url을 WebView로 열기 (0) | 2020.02.03 |