简体   繁体   English

使用imageButtons切换活动。 如何在按下按钮的第二个活动中切换图像?

[英]Using imageButtons to switch activities. How can I switch image in second activity based on button pressed?

I am just starting to dabble with Android development. 我刚刚开始涉足Android开发。 Right now, I have a program in Android Studio that has two columns of images about 6 images each. 现在,我在Android Studio中有一个程序,该程序具有两列图像,每列约6张图像。 I want to make it so if I click that image, it will appear in the new activity. 我要这样做,所以如果我单击该图像,它将显示在新活动中。 Right now, I am just loading the layout like this: 现在,我正在像这样加载布局:

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    String name = intent.getStringExtra(Main.CHAMPION_NAME);
    setContentView(R.layout.activity_champion_page);

}

With the layout in setContentView looking like this: setContentView中的布局如下所示:

<LinearLayout 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="atree496.leaguestats.ChampionPage">


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/azir"
        android:id="@+id/imageView" />
</LinearLayout>

As of now, it opens to the one image because that is how it is set up, but I want to be able to have the image be the one that I click. 到目前为止,由于它是这样设置的,因此它打开到一张图像,但是我希望能够使该图像成为我单击的图像。 What changes do I need to do in order to achieve this? 为此,我需要做哪些更改? Again, I am new to this. 再次,我是新来的。 Thank you for any help you can give. 感谢您提供的任何帮助。

You could add to the intent the int resource of your pressed image if you give it file name to Tag attribute in xml: 如果您将所按下图像的int资源的文件名赋予xml中的T​​ag属性,则可以向其添加意图:

ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/azir"
android:tag="theImageFileName.png"
android:id="@+id/imageView" />

Then you could retreive it when pressed and add it to the Intent you are using: 然后,您可以在按下它时对其进行检索,并将其添加到您正在使用的Intent中:

 @Override
 public void onClick(View v) {
     String fileName = (String) v.getTag();
     Intent intent = new Intent(this, SecondActivity.class);
     intent.putExtra("fileName", fileName);
      startActivity(intent);
 }

In second activity after you initialize the secondImageView: 在初始化secondImageView之后的第二个活动中:

String fileName = getIntent().getStringExtra("fileName");
int resId = getResources().getIdentifier(fileName,"drawable", getPackageName());
secondImage.setImageDrawable(getResources().getDrawable(resId));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM