• 検索結果がありません。

リソース定義を参照するためのクラス

R.java

gen/R.java

は自動生成されるクラス

• HelloAndroid.java

は直接

main.xml

を読む のではなく

R.java

経由でアクセスする

プログラマはこのファイルを直接変更 してはならない

2011521日土曜日

文字列参照の流れ

R.java

main.xml

strings.xml

HelloAndroid.java

プログラムから直接文字列を書き換えてみる

2011521日土曜日

<?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:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello"

android:id="@+id/hello"

/>

main.xml

ソースコードから にアクセス

package us.shiroyama.google.android.test;

import android.app.Activity;

import android.os.Bundle;

import android.widget.TextView;

public class HelloAndroid extends Activity {

TextView text;

/** Called when the activity is first created. */

@Override

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

setContentView(R.layout.main);

text = (TextView) findViewById(R.id.hello);

text.setText("

直接テキストを書き換え

");

} }

HelloAndroid.java

main.xml

id

を追加すると

R.java

は自動的に再生成される

Ctrl + Shift + o

(Mac

Command + Shift + o)

でインポート追加

2011521日土曜日

これじゃあまりにもさみしい ボタンを追加してみよう!

2011521日土曜日

… の前に、ボタンのテキストを

リソースファイルに追加

追加された

strings.xml

2011521日土曜日

ドラッグ ドロップ

右クリック

main.xml

main.xml

2011521日土曜日

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

<resources>

<string name="hello">Hello World, HelloAndroid!</string>

<string name="app_name">HelloAndroid</string>

<string name="button01">THIS IS BUTTON</string>

</resources>

res/values/strings.xml

<?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:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello"

android:id="@+id/hello"/>

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/button01"

android:id="@+id/button01"/>

</LinearLayout>

res/layout/main.xml

widget

をたてに配置

2011521日土曜日

ボタンをクリックしたら

テキストが変わるように修正!

package us.shiroyama.google.android.test;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.TextView;

public class HelloAndroid extends Activity { TextView text;

Button button;

/** Called when the activity is first created. */

@Override

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

setContentView(R.layout.main);

text = (TextView) findViewById(R.id.hello);

button = (Button) findViewById(R.id.button01);

button.setOnClickListener(new OnClickListener() { @Override

public void onClick(View v) {

text.setText("

クリックされました。

");

} });

} }

HelloAndroid.java

ClickListener

の登録

2011521日土曜日

Android 〜基礎講座〜

2011521日土曜日

Activity

Activity

画面表示のための基本的な機能の単位

Activity

クラスを継承することで、画面表

示の煩雑な処理をほとんど省略できる

他のアクティビティを起動することで連携 が可能

Android

アプリは

Activity

の集合

2011521日土曜日

通常の

Activity

の他にも、地図を扱うの に特化した

MapView

など多彩

メソッドをオーバーライドすることで 様々な状態での挙動を指示できる

AndroidManifest.xml

に記述しないと 使用できない

AndroidManifest.xml

アイコンや

Activity

情報などが記載

2011521日土曜日

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="us.shiroyama.google.android.test"

android:versionCode="1"

android:versionName="1.0">

<uses-sdk android:minSdkVersion="4" />

<application android:icon="@drawable/icon" android:label="@string/app_name">

<activity android:name=".HelloAndroid"

android:label="@string/app_name">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

AndroidManifest.xml

Activity のライフサイクル

2011521日土曜日

Activity のライフサイクル

前述のとおり

Android

は「1画面1

Activity

が基本」

Activity

はスタック状に積まれる

他の

Activity

やアプリとの兼ね合いで、

Activity

は絶えず状態を変化させている

onCreate()

onStart()

• onRestart()

onResume()

onPause()

onStop()

• onDestroy()

2011521日土曜日

Activity

の初回起動時に呼ばれる

初期化処理に使う

このあと必ず

onStart()

が呼ばれる

onCreate()

Activity

がユーザから見えるようになる 直前に呼ばれる

onStart()

2011521日土曜日

Activity

がユーザの入力を受け付けるよ うになった時に呼ばれる

メディア再生などによいタイミング

Activity

復活時には必ずここを通る

onResume()

Activity

がバックグラウンドになる直前 に呼ばれる

データの保存処理などはここで行う

ここから先はメモリ状況次第で予告な くプロセスが破棄される危険性がある

onPause()

2011521日土曜日

Activity

がしばらく使われないと呼ばれ る。具体的なタイミングは不確定

再び実行中になるなら

onRestart()

が、破 棄されるなら

onDestroy()

が呼ばれる

onStop()

onStop()

のあと、フォアグラウンド

Activity

が終わるなどして再び

Activity

が 表示される際に呼び出される

onRestart()

2011521日土曜日

Activity

が破棄されるタイミングで呼ば れる

これが

Activity

が受け取れる最後のメ

ソッド呼び出し

onDestroy()

まとめ

onCreate()

で初期化処理

onPause()

以降はいつプロセスが終わっ

てもおかしくないのでデータ退避

2011521日土曜日

レイアウト

レイアウト

• LinearLayout

一直線レイアウト(横・縦)

• TableLayout

テーブルタグのようなレイアウト

• RelativeLayout

ウィジェット同士の相対位置レイアウト

• FrameLayout

本来1つのウィジェットを配置するためのレイアウト

2011521日土曜日

インテント

ドキュメント内 勉強会 資料 Google Developer Group 京都 (ページ 109-146)

関連したドキュメント