libro
www.tuyano.com
Google androidプログラミング入門

IntentとActivity (5/6)

作成:2010-05-17 16:37
更新:2011-01-16 14:25

■SampleActivityのレイアウトを作成する

では、もう1つのActivityである「SampleActivity」を作りましょう。まず、レイアウトからです。これは「sample.xml」というファイルとして作成しましょう。ここでは、テキストを表示するTextViewだけを用意しておきます。

作成したAcrtityは、アプリケーションに登録をしておかなければいけません。これは「AndroidManifest.xml」というファイルに記述をします。下のリスト欄にソースコードを掲載してあります。<application>タグ内に、<activity>というタグを用意し、そこにandroid:nameandroid:labelを用意しています。これで、SampleActivityクラスがActivityとして登録されます。

また、これらで使っているテキストを保管しているリソースstrings.xmlも修正が必要ですね。以下にリストを掲載しておきます。

※プログラムリストが表示されない場合

AddBlockなどの広告ブロックツールがONになっていると、プログラムリスト等が表示されない場合があります。これらのツールをOFFにしてみてください。

●プログラム・リスト●

※sample.xmlのソースコード


<?xml version="1.0" encoding="utf-8"?>
	<LinearLayout
		xmlns:android=
		"http://schemas.android.com/apk/res/android"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content">
	<TextView
		android:layout_height="fill_parent"
		android:layout_width="fill_parent"
		android:text="@+id/text1"
		android:textSize="12pt"
		android:id="@+id/text1" />
</LinearLayout>


※AndroidManifest.xmlのソースコード

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
		package="jp.tuyano"
		android:versionCode="1"
		android:versionName="1.0">
	<application
			android:icon="@drawable/icon"
			android:label="@string/app_name">
		<activity android:name=".SampleApp"
				android:label="@string/app_name">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>

		<activity
				android:name="SampleActivity"
				android:label="@string/act_name">
		</activity>

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


※strings.xmlのソースコード


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">This is Intent Test.</string>
    <string name="app_name">AndroidApp</string>
    <string name="btn_label">Click</string>
    <string name="act_name">SampleActivity</string>
</resources>
※関連コンテンツ

「Google androidプログラミング入門」に戻る