<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="계산기 APP"
android:textSize="30sp" />
<EditText
android:id="@+id/one"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center" >
<RadioGroup
android:orientation="horizontal"
android:id="@+id/RadioGroup01"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="+"
android:textSize="30sp" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="-"
android:textSize="30sp" />
<RadioButton
android:id="@+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="X"
android:textSize="30sp" />
<RadioButton
android:id="@+id/radio4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="÷"
android:textSize="30sp" />
</RadioGroup>
</LinearLayout>
<EditText
android:id="@+id/two"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />
<Button
android:id="@+id/sum"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="계산" />
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="결과 : "
android:textSize="30sp" />
</LinearLayout>
package kr.vichara.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class TestActivity extends Activity {
int i,j,a1,a2,a3,a4,p;
String b1;
TextView result;
Button sum;
RadioGroup group;
EditText one,two;
RadioButton rb;
/*public void Process(){
switch(group.getCheckedRadioButtonId()){
case R.id.radio1:
result.setText("결과 : "+(i+j));
break;
case R.id.radio2:
result.setText("결과 : "+(i-j));
break;
case R.id.radio3:
result.setText("결과 : "+(i*j));
break;
case R.id.radio4:
result.setText("결과: "+(i/j));
break;
}
}*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
result = (TextView)findViewById(R.id.result);
sum = (Button)findViewById(R.id.sum);
one = (EditText)findViewById(R.id.one);
two = (EditText)findViewById(R.id.two);
//초기 라디오 선택
group=(RadioGroup)findViewById(R.id.RadioGroup01);
group.check(R.id.radio1);
sum.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
try{
i = Integer.parseInt(one.getText()+"");
j = Integer.parseInt(two.getText()+"");
}catch(Exception e){
result.setText("예외 발생");
}finally{
switch(group.getCheckedRadioButtonId()){
case R.id.radio1:
result.setText("결과 : "+(i+j));
break;
case R.id.radio2:
result.setText("결과 : "+(i-j));
break;
case R.id.radio3:
result.setText("결과 : "+(i*j));
break;
case R.id.radio4:
result.setText("결과: "+(i/j));
break;
}
}
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="사칙연산 계산기 APP"
android:textSize="30sp" />
<EditText
android:id="@+id/one"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />
<TextView
android:id="@+id/selection"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="50dp"
android:drawSelectorOnTop="true" />
<EditText
android:id="@+id/two"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />
<Button
android:id="@+id/sum"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="계산" />
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="결과 : "
android:textSize="30sp" />
</LinearLayout>
package kr.vichara.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
public class TestActivity extends Activity implements
AdapterView.OnItemSelectedListener{
int i, j,p;
String s;
TextView selection;
String[] items = { "+","-","X","÷"};
TextView result ;
EditText one,two;
Spinner spin;
Button sum;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
selection = (TextView) findViewById(R.id.selection);
//이벤트 소스
spin = (Spinner) findViewById(R.id.spinner);
//이벤트 소스와 이벤트 리스너가 구현된 객체 연결
spin.setOnItemSelectedListener(this);
//어댑터를 이용해 데이터 셋팅
ArrayAdapter<string> aa = new ArrayAdapter<string>(this,
android.R.layout.simple_spinner_dropdown_item, items);
//드롭다운 화면에 표시할 리소스 지정
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//ArrayAdapter를 Spinner에 등록
spin.setAdapter(aa);
sum = (Button) findViewById(R.id.sum);
one = (EditText) findViewById(R.id.one);
two = (EditText) findViewById(R.id.two);
result = (TextView) findViewById(R.id.result);
//익명내부클래스로 이벤트리스너
sum.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
try{
i = Integer.parseInt(one.getText()+"");
j = Integer.parseInt(two.getText()+"");
}catch(Exception e){
result.setText("예외 발생");
}finally{
switch((int)spin.getSelectedItemPosition()){
case 0 :
result.setText("결과 : "+(i+j));;
break;
case 1 :
result.setText("결과 : "+(i-j));
break;
case 2 :
result.setText("결과 : "+(i*j));
break;
case 3 :
if(i/j<=0){
result.setText("0보다 작은수로 나눌 수 없습니다");
}else{
result.setText("결과: "+(i/j));
break;
}
}
}
}
});
}
//이벤트 핸들러
//전달된 인자
//AdapterView<!--?--> parent : 이벤트가 발생한 Spinner 객체
//(제네릭 표현을 써야됨 쓰고싶지 않을때<!--?-->,노란줄이 가더라도 안쓰는것이 좋음) <!--?--> = Object
//view v : Spinner 하위 객체의 이벤트가 발생한 객체
//position : 이벤트가 발생한 위치
//id : position = id
public void onItemSelected(AdapterView<!--?--> parent, View v, int position,
long id) {
}
//이벤트 핸들러
public void onNothingSelected(AdapterView<!--?--> parent) {
selection.setText("");
}
}