<?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="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" />


    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" 
        android:textSize="30pt"/>

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="더하기" />

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

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

</LinearLayout>
package com.calcul;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class TestCalculatorActivity extends Activity {
    EditText edit1, edit2;
    TextView textView1;
	RadioGroup radioGroup;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        edit1=(EditText) findViewById(R.id.editText1);
        edit2=(EditText) findViewById(R.id.editText2);
        textView1=(TextView) findViewById(R.id.textView1);
        radioGroup=(RadioGroup) findViewById(R.id.radioGroup1);
        
        radioGroup.setOnCheckedChangeListener(
    		new OnCheckedChangeListener(){

				@Override
				public void onCheckedChanged(RadioGroup arg0, int arg1) {
					
					String one=edit1.getText().toString();
					String two=edit2.getText().toString();
					int x = Integer.parseInt(one);
					int y = Integer.parseInt(two);
					
					switch(arg1){
					case R.id.radio0:
						textView1.setText(x+y+"");
						break;
					case R.id.radio1:
						textView1.setText(x-y+"");
						break;
					case R.id.radio2:
						textView1.setText(x*y+"");
						break;
					}
					
				}});
    }
}







 내가 한거
package com.gusfree.cal;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class TestCalculatorActivity extends Activity {
	EditText edit1, edit2;
	TextView textView1;
	RadioGroup radioGroup;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		edit1=(EditText) findViewById(R.id.editText1);
		edit2=(EditText) findViewById(R.id.editText2);
		textView1=(TextView) findViewById(R.id.textView1);
		radioGroup=(RadioGroup) findViewById(R.id.radioGroup1);

		radioGroup.setOnCheckedChangeListener(
				new OnCheckedChangeListener(){

					@Override
					public void onCheckedChanged(RadioGroup arg0, int arg1) {

						//" a " -> trim() -> "a"
						//"   " -> trim() -> ""
						Editable editable1=edit1.getText();
						String one=editable1.toString().trim();
						//String one=edit1.getText().toString();

						String two=edit2.getText().toString().trim();

						/*if(one==null || one.length()<1){
							one="0";
						}
						if(one==null || one.length()<1){
							two="0";
						}*/
						
						int x = Integer.parseInt(one);
						int y = Integer.parseInt(two);

						switch(arg1){
						case R.id.radio0:
							if(x!=0 || y!=0){
								textView1.setText("더한값 "+(x+y)+"");
							}else{
								textView1.setText("정확한 값을 입력하시오");
							}
							break;
						case R.id.radio1:
							textView1.setText("뺀값 "+(x-y)+"");
							break;
						case R.id.radio2:
							textView1.setText("곱한값 "+(x*y)+"");
							break;
						}
					}
				});
	}
}




'Android > 2012.04월 강좌' 카테고리의 다른 글

5일차 Intent  (0) 2012.05.02
5일차 Grid  (0) 2012.05.02
5일차 계산기(내가 한것)  (0) 2012.05.02
5일차 복습  (0) 2012.05.02
4일차 다른소스 import 하기  (1) 2012.05.01

+ Recent posts