A sentimental robot

Thread를 이용한 UI 본문

Android Studio

Thread를 이용한 UI

GOD03219 2018. 2. 7. 16:14
package com.example.i310_54.myapplication08;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import java.util.Random;


public class MainActivity extends AppCompatActivity {

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

}

public void f1(View view) {

new Thread() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
Button btn1 = findViewById(R.id.b);
btn1.setText("앵~");
}
});
}
}.start();

}
}

 

package com.example.i310_54.myapplication08;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import java.util.Random;


public class MainActivity extends AppCompatActivity {

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

}

public void f1(View view) {

new Thread() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {

Button btn1 = findViewById(R.id.b);

try {
sleep(3000); // 3초 후 버튼 Text 바뀜
btn1.setText("newText");

} catch (InterruptedException e) {
e.printStackTrace();
}

}
});
}
}.start();

}
}

'Android Studio' 카테고리의 다른 글

AsyncTask  (0) 2018.12.14
Menu  (0) 2018.02.07
My xml  (0) 2018.02.06
Customizing  (0) 2018.02.06
OnTouch  (0) 2018.02.06