
1. activity_main.xml 코드
<androidx.appcompat.widget.LinearLayoutCompat
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">
<EditText
android:id="@+id/et1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="숫자 1" />
<EditText
android:id="@+id/et2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="숫자 2" />
<Button
android:id="@+id/btnAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="더하기" />
<Button
android:id="@+id/btnMinus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="빼기" />
<Button
android:id="@+id/btnDup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="곱하기" />
<Button
android:id="@+id/btnDivide"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="나누기" />
<TextView
android:id="@+id/tvResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:textColor="@android:color/holo_red_light"
android:layout_margin="10dp"
android:text="계산 결과 : " />
</androidx.appcompat.widget.LinearLayoutCompat>
2. MainActivity.java 코드
public class MainActivity extends AppCompatActivity{
private EditText et1, et2;
private Button btnAdd, btnMinus, btnDup, btnDivide;
private TextView tvResult;
private String str1, str2;
private Integer result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
initLr();
}
@Override
public void init() {
setTitle("초간단 계산기");
et1 = findViewById(R.id.et1);
et2 = findViewById(R.id.et2);
btnAdd = findViewById(R.id.btnAdd);
btnMinus = findViewById(R.id.btnMinus);
btnDup = findViewById(R.id.btnDup);
btnDivide = findViewById(R.id.btnDivide);
tvResult = findViewById(R.id.tvResult);
}
@Override
public void initLr() {
btnAdd.setOnClickListener(v -> {
str1 = et1.getText().toString();
str2 = et2.getText().toString();
result = Integer.parseInt(str1) + Integer.parseInt(str2);
tvResult.setText("계산 결과 : " + result.toString());
});
btnMinus.setOnClickListener(v -> {
str1 = et1.getText().toString();
str2 = et2.getText().toString();
result = Integer.parseInt(str1) - Integer.parseInt(str2);
tvResult.setText("계산 결과 : " + result.toString());
});
btnDup.setOnClickListener(v -> {
str1 = et1.getText().toString();
str2 = et2.getText().toString();
result = Integer.parseInt(str1) * Integer.parseInt(str2);
tvResult.setText("계산 결과 : " + result.toString());
});
btnDivide.setOnClickListener(v -> {
str1 = et1.getText().toString();
str2 = et2.getText().toString();
result = Integer.parseInt(str1) / Integer.parseInt(str2);
tvResult.setText("계산 결과 : " + result.toString());
});
}
}