본문 바로가기

실시간 운영체제

[실시간 운영체제] 온도 변환기

Celsius 에서 Fahrenheit 로 온도 변환기 입니다.

 

package com.example.temperature_converter;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private EditText myText;
    private RadioButton RB_to_F, RB_to_C;
    private RadioGroup my_RB_group;
    private TextView result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myText = (EditText) findViewById(R.id.myText);

        RB_to_F = (RadioButton) findViewById(R.id.rb_to_F);
        RB_to_C = (RadioButton) findViewById(R.id.rb_to_C);

        my_RB_group = (RadioGroup) findViewById(R.id.radioGroup);

        result = (TextView) findViewById(R.id.result_design);
    }

    public void click_convert(View view)
    {
        String input_T_string = myText.getText().toString();

        if (input_T_string.length() != 0) {

            float input_temperature = Float.parseFloat(input_T_string);
            float output_temperature;

            if (RB_to_F.isChecked()) {
                output_temperature = (input_temperature * 9 / 5) + 32;
                result.setText("C = " + String.valueOf(output_temperature) + "F");
            } else if (RB_to_C.isChecked()){
                output_temperature = (input_temperature - 32) * 5 / 9;
                result.setText("F = " + String.valueOf(output_temperature) + "C");
            }
            else{
                result.setText("add number and click button");
            }
        }

    }

    public void click_clear(View view)
    {
//        RB_to_F.setChecked(false);
//        RB_to_C.setChecked(false);

        my_RB_group.clearCheck();

        myText.setText("");
        result.setText("");
    }
}