배열과 테이블 위젯 이용한 계산기, 숫자버튼 예제
2019. 10. 21. 15:40
Programing/Android
1. activity_main.xml
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="5"
android:orientation="vertical"
android:rowCount="9">
<EditText
android:id="@+id/Edit1"
android:layout_columnSpan="5"
android:layout_gravity="fill_horizontal"
android:hint="숫자1 입력" />
<EditText
android:id="@+id/Edit2"
android:layout_columnSpan="5"
android:layout_gravity="fill_horizontal"
android:hint="숫자2 입력" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/BtnNum0"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="0" />
<Button
android:id="@+id/BtnNum1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="1" />
<Button
android:id="@+id/BtnNum2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2" />
<Button
android:id="@+id/BtnNum3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="3" />
<Button
android:id="@+id/BtnNum4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="4" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/BtnNum5"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="5" />
<Button
android:id="@+id/BtnNum6"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="6" />
<Button
android:id="@+id/BtnNum7"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="7" />
<Button
android:id="@+id/BtnNum8"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="8" />
<Button
android:id="@+id/BtnNum9"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="9" />
</LinearLayout>
<Button
android:id="@+id/BtnAdd"
android:layout_column="0"
android:layout_columnSpan="5"
android:layout_gravity="fill_horizontal"
android:layout_margin="5dp"
android:layout_row="4"
android:text="더하기" />
<Button
android:id="@+id/BtnSub"
android:layout_column="0"
android:layout_columnSpan="5"
android:layout_gravity="fill_horizontal"
android:layout_margin="5dp"
android:layout_row="5"
android:text="빼기" />
<Button
android:id="@+id/BtnMul"
android:layout_column="0"
android:layout_columnSpan="5"
android:layout_gravity="fill_horizontal"
android:layout_margin="5dp"
android:layout_row="6"
android:text="곱하기" />
<Button
android:id="@+id/BtnDiv"
android:layout_column="0"
android:layout_columnSpan="5"
android:layout_gravity="fill_horizontal"
android:layout_margin="5dp"
android:layout_row="7"
android:text="나누기" />
<TextView
android:id="@+id/TextResult"
android:layout_column="0"
android:layout_columnSpan="5"
android:layout_gravity="fill_horizontal"
android:layout_margin="5dp"
android:layout_row="8"
android:text="계산 결과 : "
android:textColor="#FF0000"
android:textSize="20dp" />
</GridLayout>
2. MainActivity.java
EditText edit1, edit2;
Button btnAdd, btnSub, btnMul, btnDiv;
TextView textResult;
String num1, num2;
Integer result;
// 10개 숫자 버튼 배열
Button[] numButtons = new Button[10];
// 10개 숫자 버튼의 id 값 배열
Integer[] 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 };
int i; // 증가값 용도
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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);
btnAdd.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View arg0, MotionEvent arg1) {
num1 = edit1.getText().toString();
num2 = edit2.getText().toString();
result = Integer.parseInt(num1) + Integer.parseInt(num2);
textResult.setText("계산 결과 : " + result.toString());
return false;
}
});
btnSub.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View arg0, MotionEvent arg1) {
num1 = edit1.getText().toString();
num2 = edit2.getText().toString();
result = Integer.parseInt(num1) - Integer.parseInt(num2);
textResult.setText("계산 결과 : " + result.toString());
return false;
}
});
btnMul.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View arg0, MotionEvent arg1) {
num1 = edit1.getText().toString();
num2 = edit2.getText().toString();
result = Integer.parseInt(num1) * Integer.parseInt(num2);
textResult.setText("계산 결과 : " + result.toString());
return false;
}
});
btnDiv.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View arg0, MotionEvent arg1) {
num1 = edit1.getText().toString();
num2 = edit2.getText().toString();
result = Integer.parseInt(num1) / Integer.parseInt(num2);
textResult.setText("계산 결과 : " + result.toString());
return false;
}
});
// 숫자 버튼 10개를 대입
for (i = 0; i < numBtnIDs.length; i++) {
numButtons[i] = (Button) findViewById(numBtnIDs[i]);
}
// 숫자 버튼 10개에 대해서 클릭이벤트 처리
for (i = 0; i < numBtnIDs.length; i++) {
final int index; // 주의! 꼭 필요함..
index = i;
numButtons[index].setOnClickListener(new View.OnClickListener() {
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();
}
}
});
}
}
}
'Programing > Android' 카테고리의 다른 글
FCM 앱 알람 (0) | 2019.11.02 |
---|---|
커스텀 토스트, 커스텀 다이얼로그 (0) | 2019.10.23 |
탭호스트 (0) | 2019.10.21 |
날짜 시간 위젯 (0) | 2019.10.21 |
ContextMenu (0) | 2019.10.21 |