C/C++プログラマの管理者が, Androidプログラムにチャレンジ. AndroidプログラミングのTipsをメモっていく予定です.

画面の遷移は, 元画面(メイン画面)のアクティビティから次画面(サブ画面)のアクティビティを起動することで実現できる.


            ↓  ↑


他の画面に切り替える

  • MainActiviy.java
    • インテントのインスタンスを生成する. ここでは, 元画面のアクティビティのインスタンスと次画面のアクティビティのクラスを明示的に指定する.
    • startActivityメソッドを使って, 次画面のアクティビティを起動する.
package com.moonlight_aska.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) {
        // TODO Auto-generated method stub
        // インテントのインスタンス生成
        Intent intent = new Intent(MainActivity.this, SubActivity.class);
        // 次画面のアクティビティ起動
        startActivity(intent);
      }
    });
  }
}
<?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
    • 後述の「元の画面に戻る」参照.
  • AndroidManifest.xml
    • 次画面のアクティビティを登録する.
<?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">
  <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>
  <uses-sdk android:minSdkVersion="4" />
</manifest>

元の画面に戻る

  • SubActivity.java
    • 元の画面に戻るタイミングで, finishメソッドを呼ぶ.
package com.moonlight_aska.android.mainactivity;

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

public class SubActivity extends Activity {
  /** Called when the activity is first created. */
  @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) {
        // TODO Auto-generated method stub
        // 次画面のアクティビティ終了
        finish();
      }
    });
  }
}
<?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>



このページへのコメント

askaです.

端末やエミュレータ内でのエラーは LogCat で確認することが
できます.
今回のような症状であればエラーが起きたところでスタック
トレースが表示されると思うので, それをみればソースコードの
どの部分でエラーが発生したがわかると思います.

0
Posted by aska 2014年05月30日(金) 23:27:20 返信

こんばんは

参考にさせていただいてます。

今回、このページを写して実行してみたのですがエミュレータで"Unfortunatly ○○ has stopped."と出てしまい先に進めません…。どのようなことが考えられるでしょうか。(ソース自体にはエラーの表示はありません。)

0
Posted by 初心者 2014年05月30日(金) 00:00:33 返信

夜遅い時間にかかわらず返信ありがとうございます。
難しく考えてました><
ASKさんのおかげで解決することができました!
ありがとうございました。

0
Posted by Rua 2014年01月24日(金) 01:31:33 返信

askaです.

Buttonの代わりにImageButtonを使用すればよいです.
http://seesaawiki.jp/w/moonlight_aska/d/%A5%A4%A5%E1%A1%BC%A5%B8%A5%DC%A5%BF%A5%F3%A4%F2%BA%EE%C0%AE%A4%B9%A4%EB

0
Posted by aska 2014年01月24日(金) 00:53:06 返信

こんばんわ
返信ありがとうございます。
出来たのですがボタンをイメージボタンにして画面を移行する方法はないでしょうか?
返信よろしくお願いいたします。

2
Posted by Rua 2014年01月24日(金) 00:00:03 返信

コメントをかく


「http://」を含む投稿は禁止されています。

利用規約をご確認のうえご記入下さい

Menu


逆引き(基礎編)

画面表示/操作(49)

フラグメント(1)

逆引き(応用編)

Firebase(2)

AD



管理人/副管理人のみ編集できます