试图在android studio中使用image进行textView更改

时间:2015-10-23 13:59:47

标签: android android-studio android-imageview textview

我有一个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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

     @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,其中每个图像都有一个文本(或一个Model类,其中Image和Text作为字段)。因此,对于每个图像,您可以获取文本并设置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);