Androidで標準APIのFragmentを使用し、Fragmentの有無に応じ処理を変えます。
【手順1】
1.「Androidプロジェクトの作成・実行方法(バージョン別一覧)」のAndroidプロジェクトの作成手順で、「AndroidSample53-004-FragmentExist」といプロジェクトを作成。
※プロジェクトの設定は以下の通り。
※他の項目は任意。
| 項目名 | 項目に設定する値 |
| アプリケーション名(Application Name) | com.example.androidsample53_004 |
| プロジェクト名(Project Name) | AndroidSample53-004-FragmentExist |
| パッケージ名(Package Name) | com.example.androidsample53_004 |
| 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_004"
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-004"
android:theme="@style/AppTheme" >
<activity android:name="com.example.androidsample53_004.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" >
<Button
android:id="@+id/buttonNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickNotification"
android:text="通知" />
<Button
android:id="@+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickAdd"
android:text="追加" />
<Button
android:id="@+id/buttonRemove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:onClick="onClickRemove"
android:text="削除" />
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</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" >
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello"
android:textColor="@android:color/black" />
</LinearLayout>
3.「Ctrl+Shift+F」を押し、コードをフォーマッティング。4.「Ctrl+S」を押し、ファイルを保存。
【手順6】
1.「src/com/example/androidsample53_004/MainActivity.java」は以下の通り。
package com.example.androidsample53_004;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final String TAG_FRAGMENT1 = "fragment1";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClickNotification(View view) {
FragmentManager fragmentManager = getFragmentManager();
Fragment1 fragment1 = (Fragment1) fragmentManager.findFragmentByTag(TAG_FRAGMENT1);
if (fragment1 != null) {
fragment1.setText("World");
} else {
Toast.makeText(this, "World", Toast.LENGTH_SHORT).show();
}
}
public void onClickAdd(View view) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment1 fragment1 = new Fragment1();
fragmentTransaction.add(R.id.fragment_container, fragment1, TAG_FRAGMENT1);
fragmentTransaction.commit();
findViewById(R.id.buttonAdd).setEnabled(false);
findViewById(R.id.buttonRemove).setEnabled(true);
}
public void onClickRemove(View view) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment1 fragment1 = (Fragment1) fragmentManager.findFragmentByTag(TAG_FRAGMENT1);
if (fragment1 != null) {
fragmentTransaction.remove(fragment1);
}
fragmentTransaction.commit();
findViewById(R.id.buttonAdd).setEnabled(true);
findViewById(R.id.buttonRemove).setEnabled(false);
}
}
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。3.「Ctrl+S」を押し、ファイルを保存。
【手順7】
1.「src/com/example/androidsample53_004/Fragment1.java」は以下の通り。
package com.example.androidsample53_004;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
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);
}
public void setText(String text) {
((TextView) getActivity().findViewById(R.id.text)).setText(text);
}
}
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。3.「Ctrl+S」を押し、ファイルを保存。
【手順8】
1.「Androidプロジェクトの作成・実行方法(バージョン別一覧)」のAndroidプロジェクトの実行手順で、実行。
【手順9】
1.以下の様に表示されれば成功です。
Fragmentが無い場合はToastで表示されます。
以上です。


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