728x90
# 실행 화면
# activity_main.xml
<?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/editTextNumber1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<EditText
android:id="@+id/editTextNumber2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/radio_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="더하기" />
<RadioButton
android:id="@+id/radio_sub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="빼기" />
<RadioButton
android:id="@+id/radio_mul"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="곱하기" />
<RadioButton
android:id="@+id/radio_div"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="나누기" />
</RadioGroup>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClicked"
android:text="계산하기" />
</LinearLayout>
# MainActivity.java
package kr.ac.skuniv.radiobuttontest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
int result = 0;
EditText eText1, eText2;
RadioButton rButton1, rButton2, rButton3, rButton4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
eText1 = (EditText) findViewById(R.id.editTextNumber1);
eText2 = (EditText) findViewById(R.id.editTextNumber2);
eText1.setInputType(0);
eText2.setInputType(0);
rButton1 = (RadioButton) findViewById(R.id.radio_add);
rButton2 = (RadioButton) findViewById(R.id.radio_sub);
rButton3 = (RadioButton) findViewById(R.id.radio_mul);
rButton4 = (RadioButton) findViewById(R.id.radio_div);
}
public void onClicked(View view){
switch(view.getId()) {
case R.id.button:
if (rButton1.isChecked()) {
result = Integer.parseInt(eText1.getText().toString()) + Integer.parseInt(eText2.getText().toString());
Toast.makeText(getApplicationContext(), "결과 " + result, Toast.LENGTH_SHORT).show();
} else if (rButton2.isChecked()) {
result = Integer.parseInt(eText1.getText().toString()) - Integer.parseInt(eText2.getText().toString());
Toast.makeText(getApplicationContext(), "결과 " + result, Toast.LENGTH_SHORT).show();
} else if (rButton3.isChecked()) {
result = Integer.parseInt(eText1.getText().toString()) * Integer.parseInt(eText2.getText().toString());
Toast.makeText(getApplicationContext(), "결과 " + result, Toast.LENGTH_SHORT).show();
} else {
result = Integer.parseInt(eText1.getText().toString()) / Integer.parseInt(eText2.getText().toString());
Toast.makeText(getApplicationContext(), "결과 " + result, Toast.LENGTH_SHORT).show();
}
}
}
}
반응형
'전공 공부 > 안드로이드프로그래밍' 카테고리의 다른 글
CheckBox, RadioButton (선택 항목 사진 출력) (0) | 2021.01.08 |
---|---|
2개 액티비티 (그림 선택하면 액티비티에 확대되어 정보 출력) (0) | 2021.01.07 |
버튼 (사진 회전) (0) | 2021.01.07 |
Toast 메시지 (0) | 2021.01.07 |
LinearLayout (버튼 배열) (0) | 2021.01.07 |