最近の更新

2015年6月22日月曜日

Fragmentのイベントを他のFragmentに伝える方法(直接View参照)

【目的】
Androidで標準APIのFragmentを使用し、Fragmentのイベントを他のFragmentに伝えます。(直接View参照)



【手順1】
1.「Androidプロジェクトの作成・実行方法(バージョン別一覧)」のAndroidプロジェクトの作成手順で、「AndroidSample53-006-FragmentView」といプロジェクトを作成。
※プロジェクトの設定は以下の通り。
※他の項目は任意。
項目名 項目に設定する値
アプリケーション名(Application Name) com.example.androidsample53_006
プロジェクト名(Project Name) AndroidSample53-006-FragmentView
パッケージ名(Package Name) com.example.androidsample53_006
Minimum Required SDK API 14
Target SDK API 14
Compile With API 14
Theme Theme Holo Light



【手順2】
1.「AndroidManifest.xml」は以下の通り。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidsample53_006"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="AndroidSample53-006"
        android:theme="@style/AppTheme" >
        <activity android:name="com.example.androidsample53_006.MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。
3.「Ctrl+S」を押し、ファイルを保存。



【手順3】
1.「res/values/styles.xml」は以下の通り。
<resources>

    <style name="AppTheme" parent="android:Theme.Holo.Light" ></style>

</resources>
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。
3.「Ctrl+S」を押し、ファイルを保存。



【手順4】
1.「res/layout/activity_main.xml」は以下の通り。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.androidsample53_006.Fragment1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <fragment
        android:id="@+id/fragment2"
        android:name="com.example.androidsample53_006.Fragment2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

</LinearLayout>
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。
3.「Ctrl+S」を押し、ファイルを保存。



【手順5】
1.「任意のファイルの作成方法」の手順で「res/layout/fragment1.xml」を作成。
2.「res/layout/fragment1.xml」は以下の通り。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="変更" />

</LinearLayout>
3.「Ctrl+Shift+F」を押し、コードをフォーマッティング。
4.「Ctrl+S」を押し、ファイルを保存。



【手順6】
1.「任意のファイルの作成方法」の手順で「res/layout/fragment2.xml」を作成。
2.「res/layout/fragment2.xml」は以下の通り。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello"
        android:textColor="@android:color/white" />

</LinearLayout>
3.「Ctrl+Shift+F」を押し、コードをフォーマッティング。
4.「Ctrl+S」を押し、ファイルを保存。



【手順7】
1.「src/com/example/androidsample53_006/MainActivity.java」は以下の通り。
package com.example.androidsample53_006;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。
3.「Ctrl+S」を押し、ファイルを保存。



【手順8】
1.「src/com/example/androidsample53_006/Fragment1.java」は以下の通り。
package com.example.androidsample53_006;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class Fragment1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment1, container, false);
    }

    @Override
    public void onStart() {
        super.onStart();
        Button button = (Button) getActivity().findViewById(R.id.button);
        final TextView text = (TextView) getActivity().findViewById(R.id.text);

        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                text.setText("World");
            }
        });
    }
}
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。
3.「Ctrl+S」を押し、ファイルを保存。



【手順9】
1.「src/com/example/androidsample53_006/Fragment2.java」は以下の通り。
package com.example.androidsample53_006;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment2 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment2, container, false);
    }
}
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。
3.「Ctrl+S」を押し、ファイルを保存。



【手順10】
1.「Androidプロジェクトの作成・実行方法(バージョン別一覧)」のAndroidプロジェクトの実行手順で、実行。



【手順11】
1.以下の様に表示されれば成功です。

















































以上です。

0 件のコメント:

コメントを投稿

注: コメントを投稿できるのは、このブログのメンバーだけです。

関連記事