ソース掲示板




すべてから検索

キーワード   条件 表示 現行ログ 過去ログ トピックス 名前 本文
fragment 要素で、画面定義と処理をひとまとめにする
日時: 2016/10/03 15:16
名前: lightbox



拡張子:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:padding="16dp"
              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.ButtonsUpperside"
                android:id="@+id/fragment1"
                tools:layout="@layout/buttons"/>

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/editText1"/>

            <fragment
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:name="lightbox.june.fragmenttest.ButtonsUnderside"
                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>

</LinearLayout>
拡張子:
<?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/buttonLeft"
        android:layout_weight="1"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ボタン2"
        android:id="@+id/buttonRight"
        android:layout_weight="1"/>

</LinearLayout>
一つのボタン定義画面を fragment 要素で二度使い、それぞれに別々のクラスを紐づけて処理を行います

メンテナンス

MainActivity ( No.1 )
日時: 2016/10/03 15:18
名前: lightbox


日時: 2016/10/03 15:18
名前: lightbox
拡張子:
package lightbox.june.fragmenttest;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

public class MainActivity extends AppCompatActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
		// Android 入力システム初期処理
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

		setContentView(R.layout.activity_main);

	}

}
このアーティクルの参照用URLをクリップボードにコピー メンテナンス
上側のボタンの処理 : lightbox.june.fragmenttest.ButtonsUpperside ( No.2 )
日時: 2016/10/03 15:21
名前: lightbox
拡張子:
package lightbox.june.fragmenttest;

import android.app.Fragment;
import android.app.FragmentManager;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

// **************************
// 画面で使われる場所が定義
// ( activity_main.xml )
// ※ 上段のボタン
// **************************
public class ButtonsUpperside 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.buttonLeft).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				EditText et = (EditText) getActivity().findViewById(R.id.editText1);
				et.setText("最初のフラグメントから、一つ目のボタンを使う");
			}
		});

		// **************************
		// 上段右ボタンの処理
		// **************************
		view.findViewById(R.id.buttonRight).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.buttonLeft);
					// 下段の左ボタンのテキストを赤に
					button.setTextColor(Color.parseColor("#FF0000"));
				}

			}
		});

		return view;

	}

}

このアーティクルの参照用URLをクリップボードにコピー メンテナンス
下側のボタンの処理 : lightbox.june.fragmenttest.ButtonsUnderside ( No.2 ) ( No.3 )
日時: 2016/10/03 15:22
名前: lightbox
拡張子:
package lightbox.june.fragmenttest;

import android.app.Fragment;
import android.app.FragmentManager;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

// **************************
// 画面で使われる場所が定義
// ( activity_main.xml )
// ※ 下段のボタン
// **************************
public class ButtonsUnderside 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.buttonLeft).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				EditText et = (EditText) getActivity().findViewById(R.id.editText2);
				et.setText("二つ目のフラグメントから、一つ目のボタンを使う");
			}
		});

		// **************************
		// 下段右ボタンの処理
		// **************************
		view.findViewById(R.id.buttonRight).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				// **************************
				// 別のフラグメントへのアクセス
				// **************************
				// フラグメントマネージャを取得して
				FragmentManager fm = getFragmentManager();
				// 目的のフラグメントを取得
				Fragment target = fm.findFragmentById(R.id.fragment1);
				if ( target != null ) {
					// その View を取得して
					View view = target.getView();
					// 中のコンテンツを取得する
					Button button = (Button) view.findViewById(R.id.buttonLeft);
					// 上段の左ボタンのテキストを赤に
					button.setTextColor(Color.parseColor("#FF0000"));
				}

			}
		});

		return view;

	}
}

このアーティクルの参照用URLをクリップボードにコピー メンテナンス