RadioButtonTest


RadioButtonTest

문제

아무거나

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/editNum1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="numberDecimal"
        android:hint="숫자를 입력하세요" />

    <EditText
        android:id="@+id/editNum2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="numberDecimal"
        android:hint="숫자를 입력하세요" />

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RadioButton
            android:id="@+id/radioButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="더하기" />

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="빼기" />

        <RadioButton
            android:id="@+id/radioButton3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="곱하기" />

        <RadioButton
            android:id="@+id/radioButton4"
            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.a1006practice1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    double result=0;
    EditText editText;
    EditText editText2;
    RadioButton sum;
    RadioButton sub;
    RadioButton mul;
    RadioButton dev;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText=(EditText)findViewById(R.id.editNum1);
        editText2=(EditText)findViewById(R.id.editNum2);
        sum=(RadioButton) findViewById(R.id.radioButton);
        sub=(RadioButton) findViewById(R.id.radioButton2);
        mul=(RadioButton) findViewById(R.id.radioButton3);
        dev=(RadioButton) findViewById(R.id.radioButton4);
    }

    public void Onclicked(View v){
        switch (v.getId()){ // v.getId()를 통해 각 버튼의 id를 비교하여 각각 이벤트를 달리할 수 있다
            case R.id.button:
                if(sum.isChecked()){
                    result=Double.parseDouble(editText.getText().toString())+Double.parseDouble(editText2.getText().toString());
                    Toast.makeText(getApplicationContext(), "결과 : "+result,Toast.LENGTH_LONG).show();
                }
                else if(sub.isChecked()){
                    result=Double.parseDouble(editText.getText().toString())-Double.parseDouble(editText2.getText().toString());
                    Toast.makeText(getApplicationContext(), "결과 : "+result,Toast.LENGTH_LONG).show();
                }
                else if(mul.isChecked()){
                    result=Double.parseDouble(editText.getText().toString())*Double.parseDouble(editText2.getText().toString());
                    Toast.makeText(getApplicationContext(), "결과 : "+result,Toast.LENGTH_LONG).show();
                }
                else{
                    result=Double.parseDouble(editText.getText().toString())/Double.parseDouble(editText2.getText().toString());
                    Toast.makeText(getApplicationContext(), "결과 : "+result,Toast.LENGTH_LONG).show();
                }
        }
    }
}






© 2021.07. by 전은성

Powered by 전은성