ふわふわぷかぷか

最近はイラレとAeにはまってます。

ボタンを押したら、新しい画面が開くようにする。(インテントで画面遷移)

新しく画面が開くことは、画面遷移というみたいです。

インテントというものを使って画面遷移をするようです。

 

やることが沢山でした。

①新しいレイアウト(xmlファイル)を作る

②新しいアクティビティを作る

③Manifestに新しいアクティビティを記入する

④ボタンの画面遷移のイベントを作る

 

①新しいレイアウト(xmlファイル)を作る

res>layoutに新しいxmlファイルを作成します。

layoutを右クリック→新規→Android XMLファイルを選択します。

ファイルのところに名前を入力し、完了をクリックします。

今回は「sub.xml」とし、内容は↓のようにしました。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#ffffff" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="新しい画面ができたよ!" />
   
</LinearLayout>

 

②新しいアクティビティを作る

src>アプリ名の中に、新しいアクティビティを作成します。(すでにメインのアクティビティがあるのと同じ場所です)

アプリ名を右クリック→新規→クラスを選択します。

そうすると、新規javaクラスを作成しますと出るので、名前のところに新しいアクティビティのファイル名を入力し、完了をクリックします。

今回のファイル名は「SubActivity」とし、中身は↓のようにしました。

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

    public void onClick(View v) {
        // TODO 自動生成されたメソッド・スタブ 
    }}

 ①で作成したレイアウトにするために、「setContentView(R.layout.sub);」となっています。

 

③Manifestに新しいアクティビティを記入する

res>AndroidManifest.xmlに新しく作ったアクティビティを書きます。

新しくアクティビティを作成したらここに記入しいないと、画面遷移ができません。

忘れててはまりました。。。

AndroidManifest.xmlに書き足すのは、

         <activity
                android:name="新しいアクティビティの名前"
                android:label="タイトル">
        </activity

 です。タイトルはタイトルバーに表示されます。

 

AndroidManifest.xml全部だと、

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="jp.hews.helloandroid"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" android:theme="@android:style/Theme.Holo.Light">
        <activity
            android:label="@string/app_name"
            android:name=".●●●Activity" >
            <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="サブ画面">
        </activity>

    </application>
</manifest>

 

④ボタンの画面遷移のイベントを作る

メインのアクティビティで、ボタンを押したら新しいアクティビティが開くようにします。

画面遷移は、

Intent intent = new Intent();
intent.setClass(getApplicationContext(), 新しいアクティビティの名前.class);
startActivity(intent);

 で出来ます。

 

main.xmlは、ボタン1つの画面にしました。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#ffffff">

<Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ボタン" />

</LinearLayout>

 

Activityは、

public class ●●●Activity extends Activity implements OnClickListener {
    Button button1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        button1 = (Button)findViewById(R.id.button1);
        button1.setOnClickListener(this);
       
    }

    public void onClick(View v) {
        if(v == button1){
                Intent intent = new Intent();
             intent.setClass(getApplicationContext(), SubActivity.class);
             startActivity(intent);}
        }}

 こうなりました。

これで画面遷移ができるようになりました。

f:id:fuwafuwapukapuka:20140222113930j:plain →  f:id:fuwafuwapukapuka:20140222113933j:plain

 

中学生でもわかる Androidアプリ開発講座

中学生でもわかる Androidアプリ開発講座