Mm

1. AffiMS^M MVC « ' ac01 m\i Controller ^AA ' User ^ KeyEvent ' M GraphicView M^A View f Model MHAA °

public void onCreate(Bundle icicle) { super. onCreate(icicle);

gv = new Graphic View(this);

gv.build_model(); gv.requestFocus();

AHA—® GraphicView I^MAAM gv ' AWA^ build_model()®mA^ A Model ' ft Model ' $PAH :

aMMM(Recursive)^TO Model(SP±HlTO^)lftA ' AffHA

4. # User SASiBt ' KeyEvent #AT ' g^AWAAW onKeyDown() :

public boolean onKeyDown(int keyCode, KeyEvent msg) {

if(keyCode == KeyEvent.KEYCODEDPADDOWN) gv.MoveDown();

5. ' MAHS ' tPfm GraphicView | MoveDown()®^ ° ftS^I^A^ :

public void MoveDown() { target_index++; this.invalidate(); }

Affi target_index ffiJpA 1 ' MAWg^m invalidate()®^ ' ftB^g^AA SAAW onDraw()Al^A8M(Recursive)AAm Model(gpAH!f^

6. onDrawO®»« drawNode()Af#A^Af^#ffi!S-i®mi£(Node) ' ft drawNodeOASmiAiraAMAAffAA^ ° AmiAAAiffAlgn :

"last_pos" : fa^A-fflmiAlffAAMffit °

10.5 #30 : UML Statechart

TOAW^aTO^MMmM.® ° « ' £ Android gMISffffi^ i^mmmm^mu ' Android Activity ^M^FJ

Andriod

acOl press button:

3. 1 >*< + >*< 0 >*< = >mmrn •

- Er com.misoo.ks05 +: El acOl.java a LlI Cal Activity .java + ¿J Calculator .java + El Half Adder .java + El R.java + S» Android Library

o, AndroidManifest.xml

// HalfAdder.java gA®

package com.misoo.kx05;

public class HalfAdder {

public int a,b, sum, carry;

HalfAdder(){ }

}

}

Step-3: MS^S^ : Calculator > : // Calculator.java gMA®

package com.misoo.kx05;

public class Calculator {

private HalfAdder adder;

private int digit_1, digit_2, state, d;

private CalActivity ax;

Calculator(CalActivity acx){

ax = acx; adder = new HalfAdder(); this.go_state_ready(); }

void EvDigitPress(int dg){

this.d = dg;

switch(state){

case 0: go_state_first(); break;

case 1: go_state_first(); break;

case 2: go_state_second(); break;

case 3: go state second(); break;

}}

void EvPlusPress(){ if(state == 1) go_state_plus(); }

void EvAssignPress(){ if(state == 3) go_state_cal(); }

void EvCPress(){ go_state_ready(); }

private void go_state_ready() { state = 0; digit_1 = digit_2 = 0;

, ax.show("><"); }

private void go_state_first(){

state = 1;

if(d == 1) ax.show("1");

else ax.show("0");

digit_1 = d; }

private void go_state_plus(){ state = 2; }

private void go_state_second(){

state = 3;

if(d == 1) ax.show("1");

else ax.show("0");

digit_2 = d; }

private void go_state_cal(){

state = 4; adder.a = digit_1; adder.b = digit_2;

String s_carry = String.valMeO/adder.carry); String s_sum = String. valueOf(adder.sum); ax.show("["+s_carry + s_sum +"]");

Step-4: MM Activity m^A : ac01 > : // ac01.java fMAfif

package com.misoo.kx05;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.ViewGroup;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.RelativeLayout;

import android.widget.TextView;

public class ac01 extends Activity implements OnClickListener {

private final int WC = ViewGroup.LayoutParams. WRAP_CONTENT; private Button btn_1, btn_2; private RelativeLayout r_layout; private TextView tv;

@Override public void onCreate(Bundle icicle) { super.onCreate(icicle);

this.create_layout(); setContentView(r_layout); } public void show(String tx){ tv.setText(tx); } private void create_layout(){

r_layout = new RelativeLayout(this); RelativeLayout.LayoutParams param;

tv = new TextView(this); tv.setText("press button:"); tv.setId(1); param = new RelativeLayout.LayoutParams(180, WC); param.addRule(RelativeLayoutALIGN_PARENT_TOP); param.topMargin = 10; param. leftMargin = 10; r_layout.addView(tv, param);

btn_ 1 = new Button(this); btn_ 1. setId(2); btn_ 1. setText(" Calculator"); btn_ 1. setOnClickListener(this); param = new RelativeLayout.LayoutParams(WC, WC); param. addRule(RelativeLay out. BELOW, 1); param.addRule(RelativeLayout.ALIGN_LEFT, 1);

param.topMargin = 25; r_layout.addView(btn_1, param);

btn_2 = new Button(this); btn_2.setId(3); btn_2. setText("Exit"); btn_2. setOnClickListener(this);

param = new RelativeLayout.LayoutParams(WC, WC); param.addRule(RelativeLayout.R/G#T_OF, 2); param.addRule(RelativeLayoutAZ/GN_TOP, 2); param.leftMargin = 5; r_layout.addView(btn_2, param);

public void onClick(View arg0) { if(arg0 == btn_1) {

Intent cal_intent = new Intent(ac01.this, CalActivity.class); startActivity(cal_intent); } else finish();

// CalActivity.java g^fif

package com.misoo.kx05;

import android.app.Activity;

import android.graphics.Color;

import android.os.Bundle;

import android.view.KeyEvent;

import android.view.View;

import android.view.ViewGroup;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.RelativeLayout;

import android.widget.TextView;

public class CalActivity extends Activity implements OnClickListener { private final int WC = ViewGroup.LayoutParams. WRAP_CONTENT; private Button[] btn; private int curr;

private RelativeLayout r_layout; private Calculator calc; private TextView tv; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle);

btn = new Button[6]; this.create_layout(); setContentView(r_layout); calc = new Calculator(this); curr = -1; if(icicle != null) i calc.digit_l = icicle.getInt("digit_l"); calc.digit_2 = icicle.getInt("digit_2"); calc.state = icicle.getInt("state"); curr = icicle.getInt("curr");

tv.setText(icicle.getString("tv"));

public void show(String tx)i tv.setText(tx); } private void create_layout()i r_layout = new RelativeLayout(this); RelativeLayout.LayoutParams param; tv = new TextView(this); tv.setText("><"); tv.setTextColor(Color.BL^E); tv.setBackgroundColor(Color. WHITE); tv.setId(l);

param = new RelativeLayout.LayoutParams(lS0, WC); param.addRule(RelativeLayoutALIGA_PAREAT_TOP); param.topMargin = 10; param. leftMargin = 10; r_layout.addView(tv, param);

btn[0]. setText(" 1 ") ; btn[0]. setOnClickListener(this);

param = new RelativeLayout.LayoutParams(WC, WC);

param. addRule(RelativeLay out. BELOW, 1);

param.addRule(RelativeLayoutALIGA_LEFT, 1);

param. topMargin = 25;

r_layout.addView(btn[0], param);

btn[l]= new Button(this); btn[l].setId(3); btn[l].setText("0"); btn[l].setOnClickListener(this);

param = new RelativeLayout.LayoutParams(WC, WC); param. addRule(RelativeLayout.RIGHT_OF, 2); param.addRule(RelativeLayoutALIGA_TOP, 2); param. leftMargin = 5; r_layout.addView(btn[l], param);

btn[2] = new Button(this) ; btn[2]. setId(4) ; btn[2].setText("C"); btn[2].setOnClickListener(this);

param = new RelativeLayout.LayoutParams(WC, WC); param. addRule(RelativeLayout.RIGHT OF, 3); param.addRule(RelativeLayoutALIGA_TOP, 3); param. leftMargin = 5; r_layout.addView(btn[2], param); btn[3] = new Button(this);

btn[3].setId(5); btn[3].setText("+"); btn[3 ]. setOnClickListener(this); param = new RelativeLayout.LayoutParams(WC, WC); param.addRule(RelativeLayout.5£ZO^, 2); param.addRule(RelativeLayoutAZ/GN_ZEF7; 2); param. topMargin = 5; r_layout.addView(btn[3], param);

btn[4]= new Button(this); btn[4] .setId(6); btn[4] .setText("="); btn[4] .setOnClickListener(this);

param = new RelativeLayout.LayoutParams(WC, WC); param.addRule(RelativeLayout..R/G^r_OF, 5); param.addRule(RelativeLayoutAZ/GN_7OP, 5); param.leftMargin = 5; r_layout.addView(btn[4], param); btn[5] = new Button(this); btn[5].setId(7); btn[5].setText("Exit"); btn[5] .setOnClickListener(this); param = new RelativeLayout.LayoutParams(WC, WC); param.addRule(RelativeLayout..R/G^r_OF, 6); param.addRule(RelativeLayoutAZ/GN_7OP, 6); param.leftMargin = 5; r_layout.addView(btn[5], param);

public void onClick(View arg0) { if(arg0 == btn[0]) else if(arg0== btn[1]) else if(arg0== btn[2]) else if(arg0== btn[3]) else if(arg0== btn[4]) else if(arg0== btn[5]) setting_color();

public void setting_color() { for(int i = 0; i<6; i++ ) {

if(i == curr) btn[i] .setTextColor(Color..RED); else btn[i] .setTextColor(Color. ÄL4CK);

@Override public boolean onKeyDown(int keyCode, KeyEvent msg) {

if(keyCode == KeyEvent. KEYCODE1) { calc.EvDigitPress(l); curr = 0; } if (keyCode == KeyEvent.KEYCODEO) { calc.EvDigitPress(0); curr = 1;} if (keyCode == KeyEvent.KEYCODE_C) { calc.EvCPress(); curr = 2; } if (keyCode == KeyEvent.KEYCODEP) { calc.EvPlusPress(); curr = 3; }

{ calc.EvDigitPress(l); curr = 0; } {calc.EvDigitPress(0); curr = 1;} { calc.EvCPress(); curr = 2; } { calc.EvPlusPress(); curr = 3; } { calc.EvAssignPress(); curr = 4; } { curr = -1; finish();

if(keyCode == KeyEventKEYCODEEQUALS)

{ calc.EvAssignPress(); curr = 4; } if(keyCode == KeyEvent. KEYCODE_E) { curr = -1; finish(); } setting_color(); return true;

@Override public void onSaveInstanceState(Bundle icicle) { super.onCreate(icicle);

icicle.putInt("digit_1", calc.digit_1); icicle.putInt("digit_2", calc.digit_2); icicle.putInt("state", calc.state); icicle.putInt("curr", curr);

icicle.putString("tv", tv.getText().toString());

1. ymmmmmy • RgMiTMMMBiO^gMa ° ^MEMiSfAfi frmmA a m b MM(Port)0# ' Ain^MMfflfAfMn ' sum ' MMMffIS carry ° ZUTM ■

P carry

3. rnm-m Calculator ^MB^SMA^J ' AgMSTOJMA AfffifM Calculator MMM ° UML ^JH :

P carry

Ready KSJ# ' Ig^ User ßMfXE- EvDlgitPress ' Working

ffi^^ 4 ffi/JKS ' MM^KSXComposit State) = Working mmm•Mgmm 4 «sx-°

5. ±Hß*fWlnitiar^"Default"XI ° ÄÄ^ : 1 #MA Working K S0# ' FirstDigitlnputed KS ' MKSWfSMISKS (Default State) °

6. g^PM^^ ' CalActivity calc ^ > J^g calc fM^MA Ready KS '

M CalActivity M^MtA 0 M 1 füST<l>Sii<l>» ' ®

^WMftW CalActivity onClick()fP onKeyDown()®^ > Xg^W^

calc.EvDigitPress(1); ffiMAEvDlgltPress^fAIncalcfMfrAi^ACalculator^^ftEvDlgltPressO®^ :

void EvDlgltPress(lnt dg){ thls.d = dg; swltch(state){

FlrstDlgltlnputed ^B ' CalActlvlty ## User $fA<+>Ä 0

^SA<+>Ä ' CalActlvlty EvPlusPress ^{Afn calc = ftM calc MM

fST<0>» ' CalActlvlty EvDlgltPress ^{Afn calc ^ ' ^A

Calculator ft EvDlgltPress() ® ^ ' ft calc # ^ d ffi ' |||A SecondDlgltlnputed ^B ' ##$iA<=>Ä 0

' CalActlvlty EvAsslgnPress ^{Afn calc = calc

AMM SecondDlgltlnputed ^B ' MUffü Calculating ^B - -IIA Calculating ^B ' ffi&SmA =

prlvate vold go_state_cal(){

state = 4; adder.a = digit_1; adder.b = digit_2; adder.run();

String s_carry = Strlng.valueOf(adder.carry); String s_sum = Strlng.valueOf(adder.sum); ax.show("["+s_carry + s_sum +"]");

3. föST<2>i ' MHAmWISMAMP^AIHT ' $P±H(A) °

0 0

Post a comment