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();
                }
        }
    }
}
반응형
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기