note

계산기 본문

개발노트

계산기

투한 2012. 1. 7. 14:17




 main.xml
<?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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="+"
        android:textSize="30sp" />

    <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>


string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, TestActivity!</string>
    <string name="app_name">Plus Calculation App :-)</string>

</resources>


R.java
/* AUTO-GENERATED FILE.  DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */

package kr.vichara.test;

public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int ic_launcher=0x7f020000;
    }
    public static final class id {
        public static final int one=0x7f050000;
        public static final int result=0x7f050003;
        public static final int sum=0x7f050002;
        public static final int two=0x7f050001;
    }
    public static final class layout {
        public static final int main=0x7f030000;
    }
    public static final class string {
        public static final int app_name=0x7f040001;
        public static final int hello=0x7f040000;
    }
}



TestActivity.java
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.TextView;

public class TestActivity extends Activity{
	int i, j;

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

		Button sum = (Button) findViewById(R.id.sum);
		//익명내부클래스로 이벤트리스너
		sum.setOnClickListener(new Button.OnClickListener(){

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


차이점은 익명내부 클래스로 코드 간소화를 하였습니다

'개발노트' 카테고리의 다른 글

성적관리 앱2  (0) 2012.01.15
성적 관리 앱  (0) 2012.01.12
안드로이드 성적관리 아이콘  (0) 2012.01.11
사칙연산 Spinner와 Radio버튼으로 하기  (0) 2012.01.10
어플  (0) 2012.01.05