Android Studio onClick按钮可在另一个活动中显示ImageView [英] Android Studio onClick button to display ImageView in another activity

查看:875
本文介绍了Android Studio onClick按钮可在另一个活动中显示ImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了2个活动 - 一个和两个。

I have setup 2 activities - one and two .

对于活动1,我有一个 EditText 和一个按钮。当用户打开应用程序时,它将显示Activity One(就像屏幕截图一样)以提示用户键入数字。例如,1或2.

For activity 1, I have a EditText and a button. When user open the app, it will show Activity One(just like the screenshot) to prompt user to key in a number. For example, 1 or 2.

我想要做的是:我想显示 ImageView 在活动2中当用户键入 EditText 中的数字时,点击活动1中的按钮。

What I am trying to do is that: I want to display a ImageView in activity 2 when user key in a number in EditTextfollow by a click on the button in activity 1.


  • 如果用户键入1,它将在活动2中显示 1.png

  • 如果用户键入2,它将在活动2中显示 2.png

  • 如果用户键在3中,它将显示 3.png 活动2
    等......

  • If user key in 1, it will display 1.png in activity 2
  • If user key in 2, it will display 2.png in activity 2
  • If user key in 3, it will display 3.png in activity 2 etc...

图片来自 drawable 文件夹。

请参阅此屏幕截图

[![在此处输入图片说明] [1]] [1]

[![enter image description here][1]][1]

我可以通过<$ c传递整数值$ c> Intent 从活动1到2但我无法为ImageView 执行此操作。所以这意味着if else循环我已经完成了 ImageView cant显示。

I can pass the integer value through Intent from activity 1 to 2 but I can't do it for ImageView. so that means the if else loop i have already done just that the ImageView cant display.

 get_image.setBackgroundResource(R.drawable.1); //  here i can only key in 01 ( it will get image from Drawable folder 1.png). i cant put int value into it. 

或者我不应该使用get_image.setBackgroundResource?有人可以帮忙吗?我在这里坚持了一天......

Or i shouldn't use get_image.setBackgroundResource?? Anyone can help? I stuck here for 1 day...

提前感谢!

请查看截图 - > http:/ /i.stack.imgur.com/53vjy.jpg

please check screenshot -> http://i.stack.imgur.com/53vjy.jpg

推荐答案

你说你可以传递整数值活动1到2所以只需使用它来查找你的图像。

You said that you can pass integer value from activity 1 to 2 so just use that to find your image.

ImageView imageView = (ImageView)findViewById(R.id.yourImageViewId);
if(1 == yourValue) { 
//set 01.png 
} else {...}

我可能会错过一些事情,因为当你说但我不能用于imageview时我无法理解。

I may missing somethings because i can't understand when you said that "but i cant do it for imageview".

编辑:在你的附加代码之后。
因此,您必须使用资源文件名映射整数值。你不能把你的整数值放到get_image.setBackgroundResource(R.drawable.id)。
我认为在你的情况下你应该使用一个数组只需存储你需要的资源id int [] drawableIds = {R.drawable.01,R.drawable.02} 在你的Activity2
然后像这样使用 get_image.setBackgroundResource(drawableIds [yourIntegerValue-1])(当然你应该把索引从索引中取出来使用这种方法)。

after your addtional code. So you must map your integer value with your resource file name. You could not put your integer value to get_image.setBackgroundResource(R.drawable.id). I think in your situation you should use an array just store id of resource you need int[] drawableIds = {R.drawable.01, R.drawable.02} in your Activity2 and then use like this get_image.setBackgroundResource(drawableIds[yourIntegerValue-1]) (ofcourse you should take care array out of index when you use this method).

这篇关于Android Studio onClick按钮可在另一个活动中显示ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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