반응형
Notice
Recent Posts
Recent Comments
Link
불로구
안드로이드 스튜디오 - 계산기 만들기(실수, 타이머) 본문
반응형
안드로이드 계산기를 만드시는 분들께 연습 코드 제공해 드립니다ㅎ_ㅎ
- 점(.)을 포함하여 실수 계산기 가능
- 리셋과, 1번에 60초라는 제한 시간을 두었습니다.
- 결과는 반올림을 통해 소수점 2자리까지 표시
- 자바 코드 -
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
EditText edit1, edit2;
Button[] numbtn = new Button[10];
Integer[] ButtonID = {R.id.btn0, R.id.btn1, R.id.btn2, R.id.btn3, R.id.btn4,
R.id.btn5,R.id.btn6,R.id.btn7,R.id.btn8,R.id.btn9};
Button btnplus, btnminus, btnmul, btndiv, btnreset, btndot;
TextView tresult;
String num1, num2;
Double result = 0.0;
ProgressBar prog= null;
Timer timer = null;
TimerTask timerTask = null;
boolean test = false;
public void initProg(){
prog.setMax(60);
prog.setProgress(60);
}
public void startTimerThread(){
timerTask = new TimerTask(){
@Override
public void run() {
// TODO Auto-generated method stub
decreaseBar();
}
};
timer = new Timer();
timer.schedule(timerTask, 0,1000);
}
public void decreaseBar(){
runOnUiThread(
new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
int currprog=prog.getProgress();
if(currprog>0){
currprog--;
}else if(currprog==0) {
if (test == false){
Toast.makeText(getApplicationContext(), "사용시간 끝 종료", Toast.LENGTH_SHORT).show();
test = !test;
}
finish();
}
if(currprog==30){
Toast.makeText(getApplicationContext(),"사용시간 30초 남았습니다.",Toast.LENGTH_SHORT).show();
}
prog.setProgress(currprog);
}
}
);
}
@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit1 = (EditText)findViewById(R.id.editText1);
edit2 = (EditText)findViewById(R.id.editText2);
for(int i=0; i<numbtn.length; i++){
numbtn[i] = (Button)findViewById(ButtonID[i]);
}
btnplus = (Button)findViewById(R.id.btnplus);
btnminus = (Button)findViewById(R.id.btnminus);
btnmul = (Button)findViewById(R.id.btnmul);
btndiv = (Button)findViewById(R.id.btndiv);
btnreset = (Button)findViewById(R.id.btnreset);
btndot = (Button)findViewById(R.id.btndot);
tresult = (TextView)findViewById(R.id.result);
prog = (ProgressBar)findViewById(R.id.progressBar);
initProg();
startTimerThread();//타이머 시작
btnplus.setOnTouchListener(new View.OnTouchListener(){
@SuppressLint("SetTextI18n")
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
num1 = edit1.getText().toString();
num2 = edit2.getText().toString();
result = Double.parseDouble(num1) + Double.parseDouble(num2);
result = (Math.round(result*100)/100.0);
tresult.setText("계산 결과 : " + result.toString());
return false;
}
});
btnminus.setOnTouchListener(new View.OnTouchListener(){
@SuppressLint("SetTextI18n")
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
num1 = edit1.getText().toString();
num2 = edit2.getText().toString();
result = Double.parseDouble(num1) - Double.parseDouble(num2);
result = (Math.round(result*100)/100.0);
tresult.setText("계산 결과 : " + result.toString());
return false;
}
});
btnmul.setOnTouchListener(new View.OnTouchListener(){
@SuppressLint("SetTextI18n")
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
num1 = edit1.getText().toString();
num2 = edit2.getText().toString();
result = Double.parseDouble(num1) * Double.parseDouble(num2);
result = (Math.round(result*100)/100.0);
tresult.setText("계산 결과 : " + result.toString());
return false;
}
});
btndiv.setOnTouchListener(new View.OnTouchL
<?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"
android:orientation="vertical"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="첫 번째 값"
android:inputType="textPersonName" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="두 번째 값"
android:inputType="textPersonName" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4" />
<Button
android:id="@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5" />
<Button
android:id="@+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7" />
<Button
android:id="@+id/btn8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="8" />
<Button
android:id="@+id/btn9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="9"
tools:text="9" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="66dp">
<Button
android:id="@+id/btnreset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="reset" />
<Button
android:id="@+id/btn0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0" />
<Button
android:id="@+id/btndot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="." />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/btnplus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+" />
<Button
android:id="@+id/btnminus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
파트너스 활동을 통해 일정액의 수수료를 제공받을 수 있음
반응형
'프로그래밍 > 안드로이드' 카테고리의 다른 글
안드로이드 - 안전하게 암호화 하기 (0) | 2021.11.01 |
---|---|
안드로이드 스튜디오 - DataBinding & LiveData (0) | 2021.04.06 |
안드로이드 스튜디오 - 내장 데이터베이스 ROOM (0) | 2021.04.04 |
Comments