728x90
반응형
📃초 타이머 만들기
📂소스코드
📝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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="시작"
android:textSize="30sp" />
<Button
android:id="@+id/btn_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="일시정지"
android:textSize="30sp" />
<Button
android:id="@+id/btn_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="정지"
android:textSize="30sp"
android:layout_marginLeft="10dp"/>
</LinearLayout>
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="50sp" />
</LinearLayout>
📝MainActivity.java
package com.project.review01;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView tv;
int count = 0;
Handler handler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 1:
tv.setText(++count + "초");
sendEmptyMessageDelayed(1, 1000);
break;
case 2:
removeMessages(1);
removeMessages(2);
break;
case 3:
count = 0;
tv.setText("");
removeMessages(1);
removeMessages(2);
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = findViewById(R.id.tv);
findViewById(R.id.btn_start).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
handler.sendEmptyMessage(1);
v.setEnabled(false);
}
});
findViewById(R.id.btn_stop).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
handler.sendEmptyMessage(2);
findViewById(R.id.btn_start).setEnabled(true);
}
});
findViewById(R.id.btn_reset).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
handler.sendEmptyMessage(3);
findViewById(R.id.btn_start).setEnabled(true);
}
});
}
}
728x90
반응형
'App > Android Studio' 카테고리의 다른 글
Android Studio - 스레드와 핸들러 (0) | 2022.08.11 |
---|---|
Android Studio - 프로그레스바와 핸들러 (0) | 2022.08.11 |
Android Studio - 태스크, 인텐트 플래그 (0) | 2022.08.10 |
Android Studio - 사용 권한 (0) | 2022.08.10 |
Android Studio - 인텐트 필터와 암시적 인텐트 수신 (0) | 2022.08.10 |
댓글