人気ブログランキング | 話題のタグを見る
ブログトップ

智信の部屋

tomonobu.exblog.jp

プログラムに関する雑談なんかを書いていこうと思います。(android記事のまとめサイトはこちらです→http://tomonobu.rocketserver.jp/android_blog_index.php)

Android:画面遷移

複数の画面(アクティビティ)を行き来しよう!!

参考資料
 「画面遷移するAndroidアプリを作成する

 ・1枚目の画面(最初に起動される Activity)として TestActivity の実装を行う。
 ・2枚目の画像(連携を行う Activity)として TestSubActivity の実装を行う。
 ・Activity の遷移を行うためには、
  Intent を作成して startActivity() でintentの起動を行う。
 ・Activity を終了させるためには finish() を実行する。
 ・TestSubActivityをアプリケーションに登録するために、
  AndroidManifest.xml に登録する。

サンプルコード(TestActivity.java)
package com.tomonobu.test.viewtest;

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.LinearLayout;

public class TestActivity extends Activity {
private final int MP = ViewGroup.LayoutParams.MATCH_PARENT;
private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
setContentView(linearLayout);

Button button = new Button(this);
button.setText("次のページへ");
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent((Activity)v.getContext(), TestSubActivity.class);
startActivity(intent);
}
});
linearLayout.addView(button, createParam(MP, WC));
}

private LinearLayout.LayoutParams createParam(int w, int h){
return new LinearLayout.LayoutParams(w, h);
}
}


サンプルコード(TestSubActivity.java)
package com.tomonobu.test.viewtest;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

public class TestSubActivity extends Activity {
private final int MP = ViewGroup.LayoutParams.MATCH_PARENT;
private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
setContentView(linearLayout);

Button button = new Button(this);
button.setText("前のページへ");
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
linearLayout.addView(button, createParam(MP, WC));
}

private LinearLayout.LayoutParams createParam(int w, int h){
return new LinearLayout.LayoutParams(w, h);
}
}


サンプルコード(AndroidManifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tomonobu.test.viewtest"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".TestActivity"
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="TestSubActivity"></activity>
</application>
</manifest>


by Tomonobu1979 | 2011-07-27 15:05 | android

by Tomonobu1979