下一张同时带有文字的图片

next image with text in same time

我是初学者 Android 开发人员。单击一个文本视图中的下一个更改文本时,我有列表文本。我创建了 imagelist 如何在单击下一个时同时更改 imageview textview

public class Home extends Fragment implements View.OnClickListener {
int stringIdList[] = {R.string.one,R.string.two,R.string.three,R.string.four,R.string.five};
int imageIdList[] = {R.drawable.one,R.drawable.two,R.drawable.three,R.drawable.four,R.drawable.five};
int stringListCounter = 0;
int imageListCounter = 0;
TextView text21;
ImageView image2;


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

}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.home, container, false);

            text21=(TextView) rootView.findViewById(R.id.dyktext);
            image2=(ImageView) rootView.findViewById(R.id.imagedyk);

    ImageButton next = (ImageButton)rootView.findViewById(R.id.next);
    ImageButton previous = (ImageButton) rootView.findViewById(R.id.previous);

@Override
public void onClick(View v) {
    int id = v.getId();
    if(id == R.id.next && stringListCounter  < stringIdList.length - 1 ) {
        stringListCounter++;

    }else if (id == R.id.previous && stringListCounter > 0 ) {
        stringListCounter--;

    }
    text21.setText(stringIdList[stringListCounter]);


}

}

如果我理解正确,请试试这个...

@Override
public void onClick(View v) {
    int id = v.getId();
    boolean updateText = false,
            updateImage = false;

    if(id == R.id.next) {
        if (stringListCounter  < stringIdList.length - 1 ) {
            stringListCounter++;
            updateText = true;
        }
        if (imageListCounter  < imageIdList.length - 1 ) {
            imageListCounter++;
            updateImage = true;
        }
    }else if (id == R.id.previous) {
        if (stringListCounter > 0 ) {
            stringListCounter--;
            updateText = true;
        }
        if (imageListCounter > 0 ) {
            imageListCounter--;
            updateImage = true;
        }
    }

    if (updateText) {
        text21.setText(stringIdList[stringListCounter]);
    }

    if (updateImage) {
        image2.setImageResource(imageIdList[imageListCounter]);
    }

}

我不确定我是否正确理解了你的问题。 您希望在更改 TextView 中的文本的同时更改 ImageView 中的图像,对吗?

在这种情况下,您只需在 setText() 命令下方添加设置图像资源的命令即可。

你的情况:

...
text21.setText(stringIdList[stringListCounter]);
image2.setImageResource(imageIdList[stringListCounter]);

另一个提示:你应该给你的变量起有意义的名字(例如 "textViewTitle" 或 "descriptionTV" ... 而不是 "text21")

希望能帮到你!