尝试在 android studio 中使用图像更改 textView

trying to make a textView change with image in android studio

我有一个 ImageView 和一个 TextView,在它们的中间我有一个 Button,按下它会在 ImageView 中设置不同的图像。我想要实现的是按下时,我希望 TextView 也发生变化,以便每个不同的图像显示不同的文本。非常感谢对此的一些帮助。您将在下面找到代码。谢谢。

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class SecondTourActivity extends AppCompatActivity {
    private Integer images[] = {R.drawable.image5, R.drawable.image7, R.drawable.image8, R.drawable.image9,
    R.drawable.image10, R.drawable.image11, R.drawable.image12, R.drawable.image13, R.drawable.image14,
            R.drawable.image15, R.drawable.image16, R.drawable.image17, R.drawable.image18,};
    private int currImage = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second_tour);


        setInitialImage();
        setImageRotateListener();
    }
    private void setImageRotateListener() {
        final Button rotatebutton = (Button) findViewById(R.id.btnRotateImage);
        rotatebutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                currImage++;
                if (currImage == 12) {
                    currImage = 0;
                }
                setCurrentImage();
            }
        });
    }

    private void setInitialImage() {
        setCurrentImage();
    }

    private void setCurrentImage() {

        final ImageView imageView = (ImageView) findViewById(R.id.imageView);
        imageView.setImageResource(images[currImage]);

    }

}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="jamie2dog.com.listintent.SecondTourActivity"
    android:background="#020202">


    <ImageView
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:id="@+id/imageView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/image5" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="This first picture is liverpool lime street station, taken in 2008, continue to next image for more details."
        android:id="@+id/textView"
        android:layout_below="@+id/imageView"
        android:layout_centerHorizontal="true"
        android:textColor="#FFFFFF"
        android:layout_marginTop="50dp" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Next image"
        android:id="@+id/btnRotateImage"
        android:layout_below="@+id/imageView"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

     @Override
     public void onClick(View arg0) {
          currImage++;
          if (currImage == 12) {
                currImage = 0;
          }
          setCurrentImage();
          final TextView textView= (TextView) findViewById(R.id.textView);
          textView.setText("What ever Text you want to set")
     }

使用上面的代码。这个想法是拥有一个 HashMap,其中对于每个图像,您都有一个文本(或一个模型 class,其中图像和文本作为字段)。因此,对于每个图像,您都可以获取文本并为 TextView 设置文本。

做这样的事情:

    final HashMap<Integer, Integer> map = new HashMap<>();
    map.put(R.drawable.image5, R.string.text5);
    // add all other things to this map
    // obtain the map here
    int text5 = map.get(R.drawable.image5);
    String text = getResources().getString(text5);