IT/Android

android - TableLayout과 GridLayout으로 만든 버튼형 계산기

노마드오브 2018. 9. 9. 22:35

1. 파일명 : layout8.xml (TableLayout을 사용한 버튼형 계산기)

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>

<EditText
android:layout_weight = "1"
android:id="@+id/Edit1"
android:layout_span="5"
android:hint="숫자1 입력" />
</TableRow>

<TableRow>

<EditText
android:layout_weight = "1"
android:id="@+id/Edit2"
android:layout_span="5"
android:hint="숫자2 입력" />
</TableRow>

<TableRow>

<Button
android:layout_weight = "1"
android:id="@+id/BtnNum0"
android:text="0" />

<Button
android:layout_weight = "1"
android:id="@+id/BtnNum1"
android:text="1" />

<Button
android:layout_weight = "1"
android:id="@+id/BtnNum2"
android:text="2" />

<Button
android:layout_weight = "1"
android:id="@+id/BtnNum3"
android:text="3" />

<Button
android:layout_weight = "1"
android:id="@+id/BtnNum4"
android:text="4" />
</TableRow>

<TableRow>

<Button
android:layout_weight = "1"
android:id="@+id/BtnNum5"
android:text="5" />

<Button
android:layout_weight = "1"
android:id="@+id/BtnNum6"
android:text="6" />

<Button
android:layout_weight = "1"
android:id="@+id/BtnNum7"
android:text="7" />

<Button
android:layout_weight = "1"
android:id="@+id/BtnNum8"
android:text="8" />

<Button
android:layout_weight = "1"
android:id="@+id/BtnNum9"
android:text="9" />
</TableRow>

<TableRow>

<Button
android:layout_weight = "1"
android:id="@+id/BtnAdd"
android:layout_margin="5dp"
android:layout_span="5"
android:text="더하기" />
</TableRow>

<TableRow>

<Button
android:layout_weight = "1"
android:id="@+id/BtnSub"
android:layout_margin="5dp"
android:layout_span="5"
android:text="빼기" />
</TableRow>

<TableRow>

<Button
android:layout_weight = "1"
android:id="@+id/BtnMul"
android:layout_margin="5dp"
android:layout_span="5"
android:text="곱하기" />
</TableRow>

<TableRow>

<Button
android:layout_weight = "1"
android:id="@+id/BtnDiv"
android:layout_margin="5dp"
android:layout_span="5"
android:text="나누기" />
</TableRow>

<TableRow>

<TextView
android:id="@+id/TextResult"
android:layout_margin="5dp"
android:layout_span="5"
android:text="계산 결과 : "
android:textColor="#FF0000"
android:textSize="20dp" />
</TableRow>
</TableLayout>



2. 파일명 : layout10.xml (GridLayout을 사용한 버튼형 계산기)

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="5"
android:rowCount="9"
>

<EditText
android:id="@+id/Edit1"
android:layout_column="0"
android:layout_row="0"
android:layout_columnSpan="5"
android:layout_gravity="fill_horizontal"
android:hint="숫자1 입력" />


<EditText
android:id="@+id/Edit2"
android:layout_column="0"
android:layout_row="1"
android:layout_columnSpan="5"
android:layout_gravity="fill_horizontal"
android:hint="숫자2 입력" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="2"
android:layout_columnSpan="5" >

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

<Button
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/BtnNum1"
android:text="1"/>

<Button
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/BtnNum2"
android:text="2"/>

<Button
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/BtnNum3"
android:text="3"/>

<Button
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/BtnNum4"
android:text="4"/>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="3"
android:layout_columnSpan="5" >

<Button
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/BtnNum5"
android:text="5"/>

<Button
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/BtnNum6"
android:text="6"/>

<Button
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/BtnNum7"
android:text="7"/>

<Button
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/BtnNum8"
android:text="8"/>

<Button
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/BtnNum9"
android:text="9"/>

</LinearLayout>

<Button
android:id="@+id/BtnAdd"
android:layout_column="0"
android:layout_row="4"
android:layout_columnSpan="5"
android:layout_margin="5dp"
android:layout_span="5"
android:layout_gravity="fill_horizontal"
android:text="더하기" />

<Button
android:id="@+id/BtnSub"
android:layout_column="0"
android:layout_row="5"
android:layout_columnSpan="5"
android:layout_gravity="fill_horizontal"
android:layout_margin="5dp"
android:layout_span="5"
android:text="빼기" />


<Button
android:id="@+id/BtnMul"
android:layout_column="0"
android:layout_row="6"
android:layout_columnSpan="5"
android:layout_margin="5dp"
android:layout_span="5"
android:layout_gravity="fill_horizontal"
android:text="곱하기" />

<Button
android:id="@+id/BtnDiv"
android:layout_column="0"
android:layout_row="7"
android:layout_columnSpan="5"
android:layout_margin="5dp"
android:layout_span="5"
android:layout_gravity="fill_horizontal"
android:text="나누기" />


<TextView
android:id="@+id/TextResult"
android:layout_column="0"
android:layout_row="8"
android:layout_columnSpan="5"
android:layout_margin="5dp"
android:layout_span="5"
android:layout_gravity="fill_horizontal"
android:text="계산 결과 : "
android:textColor="#FF0000"
android:textSize="20dp" />

</GridLayout>



3. 파일명 : MainActivity.java

package com.example.it.myapplication5_3;

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

public class MainActivity extends AppCompatActivity {

EditText edit1, edit2;
Button btnAdd, btnSub, btnMul, btnDiv;
TextView textResult;

String num1, num2;
Button [] numButtons = new Button[10];

//R.java 자동으로 만들어지는데, 고유ID

int [] numBtnIDs = { R.id.BtnNum0, R.id.BtnNum1, R.id.BtnNum2, R.id.BtnNum3, R.id.BtnNum4, R.id.BtnNum5, R.id.BtnNum6, R.id.BtnNum7, R.id.BtnNum8, R.id.BtnNum9 };

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.layout8);
//setTitle("테이블레이아웃 계산기");

setContentView(R.layout.layout10);
setTitle("그리드레이아웃 계산기");

edit1 = (EditText) findViewById(R.id.Edit1);
edit2 = (EditText) findViewById(R.id.Edit2);

btnAdd = (Button) findViewById(R.id.BtnAdd);
btnSub = (Button) findViewById(R.id.BtnSub);
btnMul = (Button) findViewById(R.id.BtnMul);
btnDiv = (Button) findViewById(R.id.BtnDiv);

textResult = (TextView) findViewById(R.id.TextResult);

for (int i = 0; i < numBtnIDs.length; i++) {
numButtons[i] = (Button) findViewById(numBtnIDs[i]);
}

for (int i = 0; i < numBtnIDs.length; i++) {
final int index;
index = i;

numButtons[index].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (edit1.isFocused() == true) {
num1 = edit1.getText().toString() + numButtons[index].getText().toString();
edit1.setText(num1);
} else if(edit2.isFocused() == true) {
num2 = edit2.getText().toString() + numButtons[index].getText().toString();
edit2.setText(num2);
} else {
Toast.makeText(getApplicationContext(), "먼저 입력할 란을 선택하세요", Toast.LENGTH_SHORT).show();
}
}
});
}

btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1 = edit1.getText().toString();
num2 = edit2.getText().toString();

int result = Integer.parseInt(num1) + Integer.parseInt(num2);

textResult.setText("계산 결과:" + result);
}
});

btnSub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1 = edit1.getText().toString();
num2 = edit2.getText().toString();

int result = Integer.parseInt(num1) - Integer.parseInt(num2);

textResult.setText("계산 결과:" + result);
}
});


btnMul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1 = edit1.getText().toString();
num2 = edit2.getText().toString();

int result = Integer.parseInt(num1) * Integer.parseInt(num2);

textResult.setText("계산 결과:" + result);
}
});

btnDiv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1 = edit1.getText().toString();
num2 = edit2.getText().toString();

int result = Integer.parseInt(num1) / Integer.parseInt(num2);

textResult.setText("계산 결과:" + result);
}
});

}
}