Android Manifest xml activity android name Activity application

  • Slides: 13
Download presentation
7-2 アクティビティの遷移 1.新しいアクティビティ 【Android. Manifest. xml に追加】 … <activity android: name=“. Activity” /> </application>

7-2 アクティビティの遷移 1.新しいアクティビティ 【Android. Manifest. xml に追加】 … <activity android: name=“. Activity” /> </application> </manifest>

起動側プログラム(その1) ここではAct. Ex. Activity クラス package jp. sample; import java. util. *; import android.

起動側プログラム(その1) ここではAct. Ex. Activity クラス package jp. sample; import java. util. *; import android. app. *; import android. content. *; import android. os. *; import android. view. View. *; import android. widget. Adapter. View. *; public class Act. Ex. Activity extends Activity { public static int CALLED_ACT =1; List. View list. V; Button button 1, button 2; Array. List<Hash. Map<String, String>> data;

起動側プログラム(その2) ここではAct. Ex. Activity クラス /** Called when the activity is first created. */

起動側プログラム(その2) ここではAct. Ex. Activity クラス /** Called when the activity is first created. */ @Override public void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); request. Window. Feature(Window. FEATURE_NO_TITLE); //レイアウト生成 Linear. Layout LL=new Linear. Layout(this); LL. set. Orientation(Linear. Layout. VERTICAL); set. Content. View(LL); list. V=new List. View(this); // ボタン生成 button 1=new Button(this); button 1. set. Text("新規"); button 2=new Button(this); button 2. set. Text("削除"); // アダプタ作成 data = new Array. List<Hash. Map<String, String>>(); String[] str ={"Term 1", "Term 2"}; int [] ids ={android. R. id. text 1, android. R. id. text 2}; Simple. Adapter adapter= new Simple. Adapter(this, data, android. R. layout. simple_list_item_single_choice, str, ids);

起動側プログラム(その3) ここではAct. Ex. Activity クラス list. V. set. Adapter(adapter); list. V. set. Choice. Mode(List.

起動側プログラム(その3) ここではAct. Ex. Activity クラス list. V. set. Adapter(adapter); list. V. set. Choice. Mode(List. View. CHOICE_MODE_SINGLE); LL. add. View(button 1); LL. add. View(button 2); LL. add. View(list. V); list. V. set. On. Item. Click. Listener(new act. On. Item. Click. Listener()); button 1. set. On. Click. Listener(new act. On. Click. Listener 1()); button 2. set. On. Click. Listener(new act. On. Click. Listener 2()); } class act. On. Click. Listener 1 implements On. Click. Listener //新規ボタンを押したとき { public void on. Click(View v) { Intent intent=new Intent(get. Application. Context(), Activity. class); int pos = data. size(); String Term 1="Term 1"; String Term 2="Term 2"; intent. put. Extra("Term 1", Term 1); intent. put. Extra("Term 2", Term 2); intent. put. Extra("Pos", pos); start. Activity. For. Result(intent, CALLED_ACT); } }

起動側プログラム(その4) (削除ボタンを押したとき処理される) class act. On. Click. Listener 2 implements On. Click. Listener { public

起動側プログラム(その4) (削除ボタンを押したとき処理される) class act. On. Click. Listener 2 implements On. Click. Listener { public void on. Click(View v) { int pos = list. V. get. Checked. Item. Position(); if(pos>=0){ data. remove(pos); Simple. Adapter adapter=(Simple. Adapter) list. V. get. Adapter(); adapter. notify. Data. Set. Changed(); list. V. set. Item. Checked(-1, true); } } }

起動側プログラム(その5) (Listを押したしたときに処理される) class act. On. Item. Click. Listener implements On. Item. Click. Listener {

起動側プログラム(その5) (Listを押したしたときに処理される) class act. On. Item. Click. Listener implements On. Item. Click. Listener { public void on. Item. Click(Adapter. View<? >v, View iv, int pos, long id) { Intent intent=new Intent(get. Application. Context(), Activity. class); Hash. Map<String, String> hm=data. get(pos); String Term 1=hm. get("Term 1"); String Term 2=hm. get("Term 2"); intent. put. Extra("Term 1", Term 1); intent. put. Extra("Term 2", Term 2); intent. put. Extra("Pos", pos); start. Activity. For. Result(intent, CALLED_ACT); } }

起動側プログラム(その6) (結果が返ってきたときに処理される) protected void on. Activity. Result(int req. Code, int result, Intent intent) {

起動側プログラム(その6) (結果が返ってきたときに処理される) protected void on. Activity. Result(int req. Code, int result, Intent intent) { if(req. Code==CALLED_ACT && result==RESULT_OK) { String Term 1=intent. get. String. Extra("Term 1"); String Term 2=intent. get. String. Extra("Term 2"); int pos=intent. get. Int. Extra("Pos", 0); Hash. Map<String, String> hm=new Hash. Map<String, String>(); hm. put("Term 1", Term 1); hm. put("Term 2", Term 2); if(pos==data. size())data. add(pos, hm); else data. set(pos, hm); Simple. Adapter ad=(Simple. Adapter) list. V. get. Adapter(); ad. notify. Data. Set. Changed(); } } }

起動される側のプログラム(その1) ここではAct. Activity クラス package jp. sample; import android. app. *; import android. content.

起動される側のプログラム(その1) ここではAct. Activity クラス package jp. sample; import android. app. *; import android. content. *; import android. os. *; import android. view. View. *; import android. widget. *;

起動される側のプログラム(その2) ここではAct. Activity クラス public class Activity extends Activity { Edit. Text et 1,

起動される側のプログラム(その2) ここではAct. Activity クラス public class Activity extends Activity { Edit. Text et 1, et 2; Button button; String Term 1, Term 2; int pos; Intent intent; /** Called when the activity is first created. */ @Override public void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); Linear. Layout LL=new Linear. Layout(this); LL. set. Orientation(Linear. Layout. VERTICAL); set. Content. View(LL); et 1=new Edit. Text(this); et 2=new Edit. Text(this); button=new Button(this); button. set. Text("OK"); intent=get. Intent(); Term 1=intent. get. String. Extra("Term 1"); Term 2=intent. get. String. Extra("Term 2"); pos=intent. get. Int. Extra("Pos", 0); et 1. set. Text(Term 1); et 2. set. Text(Term 2); LL. add. View(et 1); LL. add. View(et 2); LL. add. View(button); button. set. On. Click. Listener(new Act. Click. Listener()); }

起動される側のプログラム(その3) ここではAct. Activity クラス class Act. Click. Listener implements On. Click. Listener { public

起動される側のプログラム(その3) ここではAct. Activity クラス class Act. Click. Listener implements On. Click. Listener { public void on. Click(View v){ intent. put. Extra("Term 1", et 1. get. Text(). to. String()); intent. put. Extra("Term 2", et 2. get. Text(). to. String()); intent. put. Extra("Pos", pos); set. Result(Activity. RESULT_OK, intent); finish(); } } }