<?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" > </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="80dp" android:text="합계" android:textSize="30dp"/> <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.gusfree.cal;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class TestCalculatorActivity extends Activity {
	int i, j;

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

		Button btn = (Button) findViewById(R.id.radio0);
		Button btn1 = (Button) findViewById(R.id.radio1);
		Button btn2 = (Button) findViewById(R.id.radio2);
		final TextView tv =(TextView)findViewById(R.id.textView1);

		btn.setOnClickListener(new Button.OnClickListener(){

			public void onClick(View v) {
				
				EditText one = (EditText) findViewById(R.id.editText1);
				EditText two = (EditText) findViewById(R.id.editText2);
				try {
					i = Integer.parseInt(one.getText() + "");
					j = Integer.parseInt(two.getText() + "");
				} catch (NumberFormatException e) {
					tv.setText("ㅡㅡ");
				} catch (Exception e) {
					tv.setText("아 뭐야");
				} finally {
					if(i != 0 && j != 0){
						tv.setText("+결과 : " + (i + j));
					}else{
						tv.setText("입력 하시오");
					}
				}
			}
		});
		btn1.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				EditText one = (EditText) findViewById(R.id.editText1);
				EditText two = (EditText) findViewById(R.id.editText2);
				try {
					i = Integer.parseInt(one.getText() + "");
					j = Integer.parseInt(two.getText() + "");
				} catch (NumberFormatException e) {
					tv.setText("ㅡㅡ");
				} catch (Exception e) {
					tv.setText("아 뭐야");
				} finally {
					if(i != 0 && j != 0){
						tv.setText("-결과 : " + (i - j));
					}else{
						tv.setText("입력 하시오");
					}
				}
			}
		});
		btn2.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				
				EditText one = (EditText) findViewById(R.id.editText1);
				EditText two = (EditText) findViewById(R.id.editText2);
				try {
					i = Integer.parseInt(one.getText() + "");
					j = Integer.parseInt(two.getText() + "");
				} catch (NumberFormatException e) {
					tv.setText("ㅡㅡ");
				} catch (Exception e) {
					tv.setText("아 뭐야");
				} finally {
					if(i != 0 && j != 0){
						tv.setText("*결과 : " + (i * j));
					}else{
						tv.setText("입력 하시오");
					}
				}
			}
		});
	}
}





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

5일차 Grid  (0) 2012.05.02
5일차 계산기  (0) 2012.05.02
5일차 복습  (0) 2012.05.02
4일차 다른소스 import 하기  (1) 2012.05.01
4일차 gallery2  (0) 2012.05.01

+ Recent posts