본문 바로가기

Development/Android

순차번호 리소스 아이디 가져오기

리소스 아이디가 btn1, btn2, btn3, .... 순차번호로 증가될때


mListImgBtn = new ArrayList<>();
Resources resources = rootView.getResources();
ImageButton imgBtn;
for(int i=0; i<10; i++)
{
    int resId = resources.getIdentifier("button"+i, "id", getActivity().getPackageName());
    imgBtn = (ImageButton) rootView.findViewById(resId);
    imgBtn.setTag(Integer.toString(i));
    imgBtn.setOnClickListener(mClickListener);
    mListImgBtn.add(i, imgBtn);
}