본문 바로가기

사이버보안학 4학년 1학기/모바일보안프로그래밍

안알못의 섭씨, 화씨 안드로이드 변환기 계산기 소스

안알못이 45분만에 해냈습니다. 여러분!


※주의※

대충 만든거라서 쓰레기 값이 다수 들어있습니다.



인증샷



activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.exam.android02.MainActivity">

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="16dp"
android:layout_marginTop="20dp"
android:ems="10"
android:inputType="textPersonName"
android:text="" />

<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="118dp"
android:ems="10"
android:inputType="textPersonName"
android:text="" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Change"
android:onClick="changeit"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/editText2"
android:layout_alignTop="@+id/editText1"
android:layout_toEndOf="@+id/editText1"
android:text="'C"
android:textSize="30sp" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/editText2"
android:layout_toEndOf="@+id/editText2"
android:text="'F"
android:textSize="30sp"
android:layout_alignBottom="@+id/editText2" />

<RadioGroup
android:layout_width="wrap_content"
android:id="@+id/radioGroup1"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/editText1"
android:layout_below="@+id/editText1">
<RadioButton
android:id="@+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="섭씨를 화씨로" />

<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="화씨를 섭씨로" />
</RadioGroup>
</RelativeLayout>


MainActivity.java

package com.exam.android02;


import android.support.v7.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.RadioGroup;

public class MainActivity extends AppCompatActivity {

private EditText edit1;
private EditText edit2;

private Button changeBtn;
private RadioButton CtoF, FtoC;

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

edit1 = (EditText) findViewById(R.id.editText1);
edit2 = (EditText) findViewById(R.id.editText2);
changeBtn = (Button) findViewById(R.id.button1);
CtoF = (RadioButton) findViewById(R.id.radioButton);
FtoC = (RadioButton) findViewById(R.id.radioButton2);
final RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);

changeBtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
String n1 = edit1.getText().toString();
String n2 = edit2.getText().toString();

int id = rg.getCheckedRadioButtonId();
//getCheckedRadioButtonId() 의 리턴값은 선택된 RadioButton 의 id 값.
RadioButton rb = (RadioButton) findViewById(id);
switch(rb.getId()) {
case R.id.radioButton:
double r = Integer.parseInt(n1) * 1.8 + 32;
edit2.setText(String.valueOf(r));
break;
case R.id.radioButton2:
double r2 = (Integer.parseInt(n2)-32) / 1.8;
edit1.setText(String.valueOf(r2));
break;
}
} // end onClick()
}); // end Listener

} // end onCreate
}