package org.ntlab.irisclient; import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.media.Image; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageButton; import androidx.fragment.app.Fragment; public class DrawingCardFragment extends Fragment { private ImageButton[] imageButtons; private Drawable[] images; // コンストラクタ public static DrawingCardFragment newInstance(String str){ // インスタンス生成 DrawingCardFragment fragment = new DrawingCardFragment(); return fragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); Resources resources = getResources(); View view = inflater.inflate(R.layout.fragment_card_drawing, container, false); // ImageButton16個の型を使いまわしやすいように配列で使用 imageButtons = new ImageButton[]{ (ImageButton) view.findViewById(R.id.imageButton0), (ImageButton) view.findViewById(R.id.imageButton1), (ImageButton) view.findViewById(R.id.imageButton2), (ImageButton) view.findViewById(R.id.imageButton3), (ImageButton) view.findViewById(R.id.imageButton4), (ImageButton) view.findViewById(R.id.imageButton5), (ImageButton) view.findViewById(R.id.imageButton6), (ImageButton) view.findViewById(R.id.imageButton7), (ImageButton) view.findViewById(R.id.imageButton8), (ImageButton) view.findViewById(R.id.imageButton9), (ImageButton) view.findViewById(R.id.imageButton10), (ImageButton) view.findViewById(R.id.imageButton11), (ImageButton) view.findViewById(R.id.imageButton12), (ImageButton) view.findViewById(R.id.imageButton13), (ImageButton) view.findViewById(R.id.imageButton14), (ImageButton) view.findViewById(R.id.imageButton15) }; // 16枚表示させるImage画像の配列 images = new Drawable[]{ resources.getDrawable(R.drawable.test02), resources.getDrawable(R.drawable.test02), resources.getDrawable(R.drawable.test02), resources.getDrawable(R.drawable.test02), resources.getDrawable(R.drawable.test02), resources.getDrawable(R.drawable.test02), resources.getDrawable(R.drawable.test02), resources.getDrawable(R.drawable.test02), resources.getDrawable(R.drawable.test02), resources.getDrawable(R.drawable.test02), resources.getDrawable(R.drawable.test02), resources.getDrawable(R.drawable.test02), resources.getDrawable(R.drawable.test02), resources.getDrawable(R.drawable.test02), resources.getDrawable(R.drawable.test02), resources.getDrawable(R.drawable.test02) }; for(int i=0; i< imageButtons.length; i++) { imageButtons[i].setOnClickListener(this::onClick); imageButtons[i].setBackground(images[i]); }; return view; } public void onClick(View v) { for(int i=0; i< imageButtons.length; i++) { if(v.getId() == imageButtons[i].getId()) { System.out.println( "タップされたボタンの配列番号:" + i); } } } }