使用imageButtons切换活动。我如何切换图像的基础上按钮$ P $次活动pssed? [英] Using imageButtons to switch activities. How can I switch image in second activity based on button pressed?

查看:122
本文介绍了使用imageButtons切换活动。我如何切换图像的基础上按钮$ P $次活动pssed?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始涉足与Android的发展。现在,我已经在Android的工作室,有大约每6幅图像的两列一个计划。我想使它所以如果我点击的形象,它会出现在新的活动。现在,我只是加载的布局是这样的:

I am just starting to dabble with Android development. Right now, I have a program in Android Studio that has two columns of images about 6 images each. 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);

}

随着的setContentView布局看起来像这样:

With the layout in setContentView looking like this:

<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.

推荐答案

您可以添加您的pressed形象的意图INT资源,如果你给它的文件名标签中的XML属性:

You could add to the intent the int resource of your pressed image if you give it file name to Tag attribute in xml:

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

然后,你可以retreive它时,pressed并将其添加到您正在使用的意图:

Then you could retreive it when pressed and add it to the Intent you are using:

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

在第二个活动在初始化secondImageView后:

In second activity after you initialize the secondImageView:

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

这篇关于使用imageButtons切换活动。我如何切换图像的基础上按钮$ P $次活动pssed?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆