«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Tags
more
Archives
Today
Total
관리 메뉴

ITGenerations

계산기 소스 본문

프로그래밍/안드로이드특강

계산기 소스

ITGenerations 2018. 1. 22. 15:42
//java

package com.example.ryan.myapplication_calculator;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

TextView textView1;
TextView textView2;
TextView textView_result;
String num1, num2;
int reNum1,reNum2;
Button btnAdd,btnSub,btnMul,btnDiv;

Button[] numButtons= new Button[10];
Integer[] numBtnIDs ={R.id.button0, R.id.button1,R.id.button2,R.id.button3,R.id.button4,
R.id.button5,R.id.button6,R.id.button7,R.id.button8,R.id.button9};
int result;
int i;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = (TextView) findViewById(R.id.textView1);
textView2 = (TextView) findViewById(R.id.textView2);
textView_result = (TextView) findViewById(R.id.textView_rsult);
Button btnAdd = (Button) findViewById(R.id.btnAdd);
Button btnSub = (Button) findViewById(R.id.btnSub);
Button btnMul = (Button) findViewById(R.id.btnMul);
Button btnDiv = (Button) findViewById(R.id.btnDiv);

num1 = textView1.getText().toString();
num2 = textView2.getText().toString();

reNum1 = Integer.parseInt(num1);
reNum2 = Integer.parseInt(num2);
for (i = 0; i < numBtnIDs.length; i++) {
numButtons[i] = (Button) findViewById(numBtnIDs[i]);
}
for (i = 0; i < numBtnIDs.length; i++) {
final int index;
index = i;

numButtons[index].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (textView1.isFocused() == true) {
num1 = textView1.getText().toString() + numButtons[index].getText().toString();
textView1.setText(num1);
} else if (textView2.isFocused() == true) {
num2 = textView2.getText().toString() + numButtons[index].getText().toString();
textView2.setText(num2);
} else {
Toast.makeText(getApplicationContext(), "먼저 에디트를 선택하세요", Toast.LENGTH_SHORT).show();
}
}
});
}
}


public void onButtonClicked(View view){




switch(view.getId()){
case R.id.btnAdd:
result=reNum1+reNum2;
Toast.makeText(getApplicationContext(), "더하기", Toast.LENGTH_SHORT).show();
break;
case R.id.btnSub:
result=reNum1-reNum2;
Toast.makeText(getApplicationContext(),"빼기",Toast.LENGTH_SHORT).show();
break;
case R.id.btnMul:
result=reNum1*reNum2;
Toast.makeText(getApplicationContext(),"곱하기",Toast.LENGTH_SHORT).show();
break;
case R.id.btnDiv:
result=reNum1/reNum2;
Toast.makeText(getApplicationContext(),"나누기",Toast.LENGTH_SHORT).show();
break;
}

}


}

//xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout 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"
tools:context="com.example.ryan.myapplication_calculator.MainActivity">

<TableRow
android:layout_width="match_parent"
android:layout_height="54dp">

<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="46dp"
android:hint="숫자1입력" />
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="61dp"
android:hint="숫자2입력" />
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/button0"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="0" />

<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />

<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />

<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3" />

<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4" />
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/button5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="5" />

<Button
android:id="@+id/button6"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="6" />

<Button
android:id="@+id/button7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="7" />

<Button
android:id="@+id/button8"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="8" />

<Button
android:id="@+id/button9"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="9" />
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/btnAdd"
android:layout_width="385dp"
android:layout_height="match_parent"
android:onClick="onButtonClicked"
android:text="더하기" />
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
android:id="@+id/btnSub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onButtonClicked"
android:text="빼기" />

</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/btnMul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onButtonClicked"
android:text="곱하기" />
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/btnDiv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onButtonClicked"
android:text="나누기" />
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/textView_rsult"
android:layout_width="wrap_content"
android:layout_height="71dp"
android:hint=" 계산결과:" />
</TableRow>
</TableLayout>