以下がボタンの画面定義ですが、<Fragment> で使用されます。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ボタン1"
android:id="@+id/button"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ボタン2"
android:id="@+id/button2"
android:layout_weight="1"/>
</LinearLayout>
この定義は、二つのフラグメントで使用されます。
public class Buttons extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.buttons, container, false);
// **************************
// 上段左ボタンの処理
// **************************
view.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText et = (EditText) getActivity().findViewById(R.id.editText);
et.setText("最初のフラグメントから、一つ目のボタンを使う");
}
});
// **************************
// 上段右ボタンの処理
// **************************
view.findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// **************************
// 別のフラグメントへのアクセス
// **************************
// フラグメントマネージャを取得して
FragmentManager fm = getFragmentManager();
// 目的のフラグメントを取得
Fragment target = fm.findFragmentById(R.id.fragment2);
if ( target != null ) {
// その View を取得して
View view = target.getView();
// 中のコンテンツを取得する
Button button = (Button) view.findViewById(R.id.button);
// 下段の左ボタンのテキストを赤に
button.setTextColor(Color.parseColor("#FF0000"));
}
}
});
return view;
}
}
二つ目です
public class Buttons2 extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.buttons, container, false);
// **************************
// 下段左ボタンの処理
// **************************
view.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText et = (EditText) getActivity().findViewById(R.id.editText2);
et.setText("二つ目のフラグメントから、一つ目のボタンを使う");
}
});
// **************************
// 下段右ボタンの処理
// **************************
view.findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// **************************
// 別のフラグメントへのアクセス
// **************************
// フラグメントマネージャを取得して
FragmentManager fm = getFragmentManager();
// 目的のフラグメントを取得
Fragment target = fm.findFragmentById(R.id.fragment);
if ( target != null ) {
// その View を取得して
View view = target.getView();
// 中のコンテンツを取得する
Button button = (Button) view.findViewById(R.id.button);
// 上段の左ボタンのテキストを赤に
button.setTextColor(Color.parseColor("#FF0000"));
}
}
});
return view;
}
}
以下は、MainActivity で使用される画面です
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="lightbox.june.fragmenttest.MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/scrollView">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="lightbox.june.fragmenttest.Buttons"
android:id="@+id/fragment"
tools:layout="@layout/buttons"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"/>
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="lightbox.june.fragmenttest.Buttons2"
android:id="@+id/fragment2"
tools:layout="@layout/buttons"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText2"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
ボタンの各処理は、フラグメントで処理され、activity_main のコンテンツは getActivity().findViewById で取得します。フラグメントから、他のフラグメントのアクセスは、 1) getFragmentManager() 2) findFragmentById() 3) getView() 4) findViewById() と、順に取得します
|
|
【Androidの最新記事】
- Android Studio : LiveDoor のお天気情報をリストビューに表示する手順
- Android のメニュー項目を条件が満たされた時のみ利用可能にする / onPrepareOptionsMenu
- Android Studio : LiveDoor のお天気情報をリストビューに表示した後、第二画面で name を変更して第一画面で再表示する手順
- Android の教科書到着しました。
- Android Studio をちょっと古い PC で使うと エミュレータが実行されなかった。
- Android Studio で、Failed to find target with hash string というエラーが出たら、Module の build.gradle を変更します
- Android の SeekBar を縦方向で使用する設定
- Android 5.1(API 22) と Android 4.4(API 19) でのそれぞれの DatePicker ダイアログの扱い
- Android 5.1(API 22) と Android 4.4(API 19) でのそれぞれの DatePicker コントロールの扱い
- Android Studio でプロジェクトを読み込むと、Error : C:\Users\User\.gradle\caches... と表示されて gradle の処理が出来なくなる場合の対処
- Android Studio 1.4.1 : Android 純正 Data Binding テンプレート
- Android アプリ作成の基礎 : PDF で問題
- Android Chrome の 謎の(?) 241文字仕様 / 241文字以上のコンテンツは文字が大きく表示される
- Android Studio : 行の途中で改行した時のインデント数を 1 にする
- Android Studio : キャラクタセット
- Android Studio : 起動時にプロジェクトを読み込まない設定
- Android Studio の SDK ヘのパスの変更
- armeabi-v7a しか動かない、ちょっと古い PC で、どーしてもエミュレータが起動しなくてやった事( 動いた、万歳 )
- Android 5.1 + Pleiades( Luna ) + ADT Plugin
- Android 5.1 エミュレータ + Android Studio 1.1.0 実行イメージ




























