Android ImageViewer以及TextView PREV和NEXT

Android ImageViewer以及TextView PREV和NEXT,android,Android,所以我在我的应用程序上制作了一个ImageViewer,它使用计数来浏览所有的图片。使用“下一步”和“上一步”按钮。现在我正试图添加一个文本视图来配合它。因此,我在下面创建了另一个textview,并尝试了以下代码: View.OnClickListener nButtonChangeImageListener = new View.OnClickListener() { public void onClick(View view) { //Inc

所以我在我的应用程序上制作了一个ImageViewer,它使用计数来浏览所有的图片。使用“下一步”和“上一步”按钮。现在我正试图添加一个文本视图来配合它。因此,我在下面创建了另一个textview,并尝试了以下代码:

   View.OnClickListener nButtonChangeImageListener = new View.OnClickListener() {

        public void onClick(View view) {
            //Increase Counter to move to next Image
            currentImage--;
            currentImage = (currentImage + images.length) % images.length;

            imageView.setImageResource(images[currentImage]);
            if(currentImage == img1){
                textView4.setText("My Awesome Text");
            }

        }
    };
更新:我试过了,但还是不行

View.OnClickListener nButtonChangeImageListener=新建视图。OnClickListener(){

})

这是“下一步”和“上一步”按钮的侦听器。我发现使用if语句,
if(currentImage==img1)
可以将textview与它自己的图像相匹配。比如说,对于img1,我将其设置为“很棒的文本”


不幸的是,它不起作用,我曾想过将
currentImage
设置为字符串,但这是不允许的。还有什么办法?或者可能是我在这段代码中做错了什么?

看来你有麻烦了,嗯。试着跳出思维定势,年轻人

我打赌你用的是一个整数来浏览你的列表。就像currentImage设置为0一样

这是另一种方法听着

设置另一个整数,例如:

private int currentText = 0;
然后:


好吧,就是这样

这是一种可能的解决方案,您可以将文本引用保存在与图像数组长度相同的数组中:

  View.OnClickListener buttonChangeImageListener = new View.OnClickListener() {


    public void onClick(View view) {
        //Increase Counter to move to next Image and Text
        current++;
        current = current % images.length;

        imageView.setImageResource(images[current]);
        textView4.setText(texts[current]);


    }
};
编辑: 定义文本数组的一种方法是:

String []texts = {"Text1","Text2",...,"TextN"};
因此,数组的字符串数将与图片数相同

另一种方法是在Resorces中定义字符串(就像您对图片所做的那样)。在res/values下添加一个新文件,并调用它,例如text.xml。 texts.xml如下所示:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="text1">This is text 1</string>
    <string name="text2">This is text 2</string>
     ....
    <string name="textN">This is text N</string>
</resources>
要获取文本:

textView4.setText(getResources().getString(texts[current]));

请详细说明-您需要为每个图像使用不同的文本,或者您需要为一些图像使用相同的文本,或者是其他内容。我需要为每个图像使用不同的文本。我找到了一个很好的回复,不过我也想听听你的回复。科伦斯先生,因为他发布的那篇文章有一点错误。喜欢与图像不匹配的文本。但是,是的,它确实在切换。这确实很短,先生,我该如何把我的文本放在列表中?对于图像,我这样做了`int[]images={R.drawable.img1,R.drawable.img2,R.drawable.img3};`我之所以能够这样做是因为它们是可绘制的,那么文本呢?
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="text1">This is text 1</string>
    <string name="text2">This is text 2</string>
     ....
    <string name="textN">This is text N</string>
</resources>
int [] texts = {R.string.text1, R.string.text2,...,R.string.textN} ;
textView4.setText(getResources().getString(texts[current]));