Sfflf

1. ftfMAMM ' i£T<MENU>M«ffi±MS Menu MTOT :

Go

Exit

MENU

ViewAct) :

-■{Jr com.misoo.kxlO + [7] acOl.java + [j] Controller.java + 2| EditAct.java + Jj PickAct.java + [7] R.java + ^ YicwAct.java + a Android Library

Step-2: MM Activity ftXS^ : ac01 • : // acOl.java fM^fif

package com.misoo.kx10; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem;

public class acOl extends Activity {

public static final int GOID = Menu.FIRST; public static final int EXITID = Menu.FIRST + 1; private Controller ctrl; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout./wa/'w); ctrl = new Controller(this);

if(icicle != null) ctrl.set_state(icicle.getChar("state"));

@Override public void onSaveInstanceState(Bundle icicle) { super.onSavelnstanceState(icicle); icicle.putInt("state", ctrl.get_state());

@Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu);

menu.add(0, GOID, 0, "Go"); menu.add(0, EXITID, 1, "Exit"); return true;

@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) {

case GOID: ctrl.Request(); break;

return super.onOptionsItemSelected(item);

@Override protected void onActivityResult(int requestCode, int resultCode, String data, Bundle extras) { if(data.contains("Next")) ctrl.Request(); else finish();

package com.misoo.kx10; import android.content.Intent;

public class Controller { private char state_var; private SuperState state; private ac01 mCtx;

Controller(ac01 ctx) { mCtx = ctx; state_var = ' I'; } public void Request() { switch(state_var) {

case 'I': this.goto_V(); break; case 'V': this.goto_E(); break; case 'E': this.goto_P(); break; case 'P': this.goto_V(); break;

state.handle();

public char get_state(){ return state_var; } public void set_state(char k){ state_var = k; }

private void goto_E(){ state_var = 'E'; private void goto_V(){ state_var = 'V'; private void goto_P(){ state_var = 'P'; private abstract class SuperState { protected abstract void handle();

private class E_State extends SuperState { public void handle(){

// Intent intent = new Intent(Intent.EDIT_ACTION, null); Intent intent = new Intent(mCtx, EditAct.class); mCtx. start ActivityForResult(intent, 100);

private class V_State extends SuperState { public void handle(){

// Intent intent = new Intent(Intent.VIEW_ACTION, null); Intent intent = new Intent(mCtx, ViewAct. class); mCtx. start ActivityForResult(intent, 101);

private class P_State extends SuperState { public void handle(){

// Intent intent = new Intent(Intent.PICK_ACTION, null); Intent intent = new Intent(mCtx, PickAct. class); mCtx. start ActivityForResult(intent, 102);

package com.misoo.kx10_dd;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class ViewAct extends Activity implements OnClickListener { Button btn, btn2; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle);

setContentView(R.layout.act);

btn.setBackgroundResource(R.drawable.x_/M^e);

btn.setText("Exit from action. View");

btn. setOnClickListener(this);

state = new E_State(); } state = new V_State(); } state = new P_State(); }

btn2 = (Button)findViewById(R.id.6tn_next); btn2. setOnClickListener(this);

public void onClick(View arg0) {

bundle.putString("DataKey", "Exit");

else bundle.putString("DataKey", "Next"); Intent mIntent = new Intent(); mIntent.putExtras(bundle); setResult(RESULT_OK, mIntent); finish();

Step-5: MM Activity iftA^BU : EditAct ' SMAfif^A : // EditAct.java MA®

package com.misoo.kx10_dd;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class EditAct extends Activity implements OnClickListener {

Button btn, btn2;

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

setContentView(R. layout. act);

btn = (Button)findViewById(R.id.6tn);

btn.setBackgroundResource(R.drawable.x_Wue);

btn.setText("Exit from action.EDIT");

btn. setOnClickListener(this);

btn2 = (Button)findViewById(R.id.6tn_next);

btn2. setOnClickListener(this);

public void onClick(View arg0) {

bundle.putString("DataKey", "Exit");

else

bundle.putString("DataKey", "Next");

Intent mIntent = new Intent();

mIntent.putExtras(bundle);

setResult(RESULT_OK, mIntent);

finish();

}}

package com.misoo.kx10_dd;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class PickAct extends Activity implements OnClickListener { /** Called when the activity is first created. */ Button btn, btn2; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle);

setContentView(R.layout.act);

btn.setBackgroundResource(R.drawable.x_5^y);

btn.setText("Exit from action.PICK");

btn.setOnClickListener(this);

btn2 = (Button)findViewById(R.id.btn_next);

btn2. setOnClickListener(this);

public void onClick(View arg0) {

bundle.putString("DataKey", "Exit");

else bundle.putString("DataKey", "Next"); Intent mIntent = new Intent(); mIntent.putExtras(bundle); setResult(RESULT_OK, mIntent); finish();

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent"

<TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text=""

<Button android:id="@+id/btn"

android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Exit"/> <Button android:id="@+id/btn_next"

android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Next"/> </LinearLayout>

public void onCreate(Bundle icicle) {

ctrl = new Controller(this);

^HX-i® Controller ffr > M^X^'I' (Iinitial)KFJ°

ctrl.set_state(icicle.getChar("state"));

^ icicle != null ' SX^M^MM onSaveInstanceState '

onSaveInstanceState ()®TO(fiP»W£AM) ' fSA^AmTfsA :

public void onSaveInstanceState (Bundle icicle) { super. onSaveInstanceState (icicle); icicle.putInt("state", ctrl.get_state());

ctrl.Request();

iA'Vfl"

IHA V_State ^ ' Request()®^ ' M^A state.handleOfsA0

6. ^ Handle()®^ffi > I-^^fSflft ViewAct ^^f^AM intent ft Activity °

7. ^ViewAct<®±®T<Next>®ffi ' IMViewActMM®^ac01(gpStep-4) >

m-m Android mmmaffi ui mmrn m 1 m m 2 m Android «ffifô^FJÎl m 3 m Android fMAff^M!

m^m Android C fifrffigm^ m 4 m ffi^W^ Android C fifr m 5 m Android C m 6 m Façade HA® JNI I^AAffiA

m=m mmmrnm^c nfr m 8 m Linter M Android MMAAA m 9 m II Linter ^^ Android ÔASA^M

mm Android rnrn^M

m 10 m Android fMAISffMfËAfô

mil m mmMMmrnmrnmAc.-m36m 307

0 0

Post a comment