目次

単純な2画面の遷移

AndroidManifest.xml

2画面目のアクティビティを登録する

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.moonlight_aska.android.mainactivity"
      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=".MainActivity"
                  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=".SubActivity"
                  android:label="@string/app_name">
        </activity>
     </application>
</manifest>

MainActivity.java

package android.mainactivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button btn = (Button)findViewById(R.id.button01_id);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // インテントのインスタンス生成
                Intent intent = new Intent(MainActivity.this, SubActivity.class);
                // 2画面目のアクティビティ起動
                startActivity(intent);
            }
        });
    }
}

main.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:id="@+id/textview01_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/textview01_label"
        />
    <Button
        android:id="@+id/button01_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button01_label"/>
</LinearLayout>

SubActivity.java

package android.mainactivity;

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

public class SubActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sub);

        Button btn = (Button)findViewById(R.id.button02_id);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 2画面目のアクティビティ終了
                finish();
            }
        });
    }
}

sub.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:id="@+id/textview02_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/textview02_label"
        />
    <Button
        android:id="@+id/button02_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button02_label"/>
</LinearLayout>

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, MainActivity!</string>
    <string name="app_name">UiFlow</string>

    <string name="textview01_label">サブ画面に切り替える</string>
    <string name="button01_label">切り替え</string>

    <string name="textview02_label">メイン画面に戻る</string>
    <string name="button02_label">戻る</string>
</resources>