android - 将变量从 Intent Extra 传递到 ImageView
安卓新人来了。我正在尝试设置一个活动来显示选定的图像。该代码的基础来自我尝试修改的《Hello,Android 第三版(实用程序员)》。
基本上,我希望能够单击一个活动中的按钮,然后启动另一个活动来更改布局以显示与该按钮关联的图像。我有几个按钮,并且希望每个按钮都能显示不同的图像。
文件(通过删除 pic3-... 简化代码):
main.xml :两个按钮的布局(pic1 和 pic2)
Main.java :包含按钮的 onclicklisteners - 带附加功能的意图(图像的文件名) )
Viewer.java : 默认由 eclipse 创建,目的是设置新布局 setContentView(R.layout.viewer);
viewer.xml:ImageView 的布局
我已将存储在 strings.xml 中的字符串用于工作(我在 eclipse 中创建一个值为 @drawable/pic1 的字符串,并为其指定一个名称 imagename,以便我可以调用 @string/imagename 作为查看器.xml 中 ImageView 的 src)。
然而,我从这个论坛上的搜索和阅读中了解到,我无法在活动中更改 strings.xml 值(我最初的想法是在 Viewer.java 中添加几行代码,将 imagename 字符串更改为传递的任何内容)通过 Intent extras,
我发现这篇文章(http://stackoverflow.com/questions/3523384/android-pass-string-from-activity-to-layout)有人试图用 TextView 做类似的事情,但是我已经尝试了这条路线,但我一直在这些行上遇到语法错误,
我真的有什么想法吗? 谢谢!
main.java
public class Main extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// click listeners
View pic1Button = findViewById(R.id.pic1_button);
pic1Button.setOnClickListener(this);
View pic2Button = findViewById(R.id.pic2_button);
pic2Button.setOnClickListener(this);
}
// ...
public void onClick(View v) {
switch (v.getId()) {
case R.id.pic1_button:
Intent l = new Intent(this, Viewer.class);
l.putExtra("imagefilename", "pic1filename");
startActivity(l);
break;
case R.id.pic2_button:
Intent i = new Intent(this, Viewer.class);
i.putExtra("imagefilename", "pic2filename");
startActivity(i);
break;
}
}
}
查看器.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src='@string/imagename'
android:scaleType="matrix">
</ImageView>
</FrameLayout>
Android newcomer here. I'm trying to setup an activity to display a selected image. The foundation of this code is from the Hello, Android 3rd edition (Pragmatic Programmer) that I have attempted to modify.
Basically, I want to be able to click a button in one activity, and have that start another activity that changes the layout to display the image associated with that button. I have several buttons, and would like each button to cause a different image to be displayed.
files (simplified code by removing pic3-...):
main.xml : layout for two buttons (pic1 and pic2)
Main.java : contains onclicklisteners for buttons - intents w/ extras (filename for image)
Viewer.java : default created by eclipse, purpose is to set new layout setContentView(R.layout.viewer);
viewer.xml : layout for ImageView
I have gotten strings stored in strings.xml to work (I create a string in eclipse with a value of @drawable/pic1 and give it a name of imagename so I can call @string/imagename for the src of ImageView in viewer.xml).
However, I have learned from searching and reading on this forum that I cannot change strings.xml values from within an activity (my original idea was to have a couple lines of code in Viewer.java that would change the imagename string to whatever was passed by the Intent extras.
I found this post (http://stackoverflow.com/questions/3523384/android-pass-string-from-activity-to-layout) where someone was trying to do a similar thing with a TextView, but I've tried that route and I keep getting syntax errors on those lines.
I'm really stuck. Any ideas?
Thanks!
main.java
public class Main extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// click listeners
View pic1Button = findViewById(R.id.pic1_button);
pic1Button.setOnClickListener(this);
View pic2Button = findViewById(R.id.pic2_button);
pic2Button.setOnClickListener(this);
}
// ...
public void onClick(View v) {
switch (v.getId()) {
case R.id.pic1_button:
Intent l = new Intent(this, Viewer.class);
l.putExtra("imagefilename", "pic1filename");
startActivity(l);
break;
case R.id.pic2_button:
Intent i = new Intent(this, Viewer.class);
i.putExtra("imagefilename", "pic2filename");
startActivity(i);
break;
}
}
}
viewer.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src='@string/imagename'
android:scaleType="matrix">
</ImageView>
</FrameLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题不是很清楚,但根据我的理解,您可以使用以下代码来实现这一目标。
AndroidManifest.xml:
Main.java:
Viewer.java:
main.xml:
viewer.xml:
在res/drawable中带有pic1.png,pic2.png和pingouin.png...
Your question is not very clear but from my understanding you could achieve that using the following code.
AndroidManifest.xml:
Main.java:
Viewer.java:
main.xml:
viewer.xml:
With pic1.png, pic2.png and pingouin.png in res/drawable…