728x90
반응형
SMALL
간단하게 쓰레드 구현방법 보겠습니다.
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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/btn_start"
android:text="스레드 시작"/>
<Button android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/btn_stop"
android:text="스레드 종료"/>
</LinearLayout>
쓰레드 시작버튼과 종료버튼을 만듭니다.
MainActivity
package com.example.threadexample;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
Button btn_start, btn_stop;
Thread thread;
boolean isThread = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_start = (Button) findViewById(R.id.btn_start);
btn_stop = (Button) findViewById(R.id.btn_stop);
btn_start.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
isThread = true;
thread = new Thread(){
public void run() {
while(isThread){
try {
sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
handler.sendEmptyMessage(0);
}
}
};
thread.start();
}
});
btn_stop.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
isThread = false;
}
});
}
private Handler handler = new Handler(){
public void handleMessage(Message msg){
Toast.makeText(getApplicationContext(),"jisay", Toast.LENGTH_SHORT).show();
}
};
}
Thread를 선언만 하고 btn_start에 onClick 메서드에서 쓰레드 객체를 만들고 handler를 통해서 메세지를 5초동안 표시합니다. btn_stop에 isThred를 false로 하면서 쓰레드가 멈춥니다. 즉 메세지는 더이상 나오지 않습니다.
핸들러는 쓰레드와 통신하며 여러 메서드를 통해 메시지를 전송할 수 있습니다.
- boolean Handler.sendEmptyMessage(int what) : 간단하게 what 값을 통해서 메시지를 보낼 때 사용합니다.
- boolean Handler.sendMessage(Message msg) : 좀더 복잡한 Message객체를 보낼 때 사용합니다.
- boolean sendMessageAtFrontOfQueue(Message msg) : 메시지는 큐에 순서대로 쌓여 처리되나 급하게 처리해야할 메시지를 우선적으로 지정할때 사용합니다.
Dialog 팝업창에 대해서 간단하게 구현해보겠습니다.
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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="dialog"
android:id="@+id/btn_dialog"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/tv_result"
android:textSize="40sp"
android:text="test"/>
</LinearLayout>
TextView를 통해서 내가 입력한 값을 나타내게 할겁니다.
MainActivity
package com.example.daiglogexample;
import android.content.DialogInterface;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
Button btn_dialog;
TextView tv_result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_dialog = (Button) findViewById(R.id.btn_dialog);
tv_result = (TextView) findViewById(R.id.tv_result);
btn_dialog.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
AlertDialog.Builder ad = new AlertDialog.Builder(MainActivity.this);
ad.setIcon(R.mipmap.ic_launcher);
ad.setTitle("제목");
ad.setMessage("jisay 입니까?");
final EditText et = new EditText(MainActivity.this);
ad.setView(et);
ad.setPositiveButton("OK", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String result = et.getText().toString();
tv_result.setText(result);
dialogInterface.dismiss();
}
});
ad.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
ad.show();
}
});
}
}
다이얼로그 팝업창에 EditText로 문자열을 입력받고 TextView에 표시하게 합니다. 마지막에 show()메서드를 꼭 써야지만
다이얼로그가 제대로 뜹니다.
그럼 결과를 보겠습니다.
728x90
반응형
LIST
'Android Studio' 카테고리의 다른 글
[Android Studio] 첫 안드로이드 앱 개발 시작해보기 - Spinner (12) (0) | 2023.11.10 |
---|---|
[Android Studio] 첫 안드로이드 앱 개발 시작해보기 - Service (11) (2) | 2023.11.09 |
[Android Studio] Android Studio + Spring Boot + mariaDB ( 로그인 ) (3) (0) | 2023.11.06 |
[Android Studio] Android Studio + Spring Boot + mariaDB ( 회원가입 ) (2) (0) | 2023.11.06 |
[Android Studio] Android Studio + Spring Boot + mariaDB ( REST API 서버 구현 ) (9) (0) | 2023.11.06 |