在运行时更改 imageButton 上的图像时遇到问题
我正在尝试在运行时更新 imageButton 的图像。我有一个 switch 语句,用于检查从另一个活动传递的 ID。我知道 switch 语句正在工作,因为正确的 ID 已传递到 TextView。
我一直在搜索并看到一些示例使用 ImageView 而其他示例使用 ImageButton。正如你在下面看到的,我已经尝试了这两种方法,但都不起作用。
XML 布局:
<ImageButton android:visibility="gone" android:id="@+id/imageButton" android:src="@drawable/defaultimage" android:layout_width="97dp" android:layout_height="95dp"></ImageButton>
<TextView android:visibility="gone" android:text="TextView" android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true"></TextView>
Java 代码:
case 1:{
// Location 1
ImageView ImageButton = (ImageView)findViewById(R.id.imageButton);
ImageButton .setImageResource(R.drawable.image1);
ImageButton .setVisibility(0);
TextView Test = (TextView)findViewById(R.id.textView);
Test.setVisibility(0);
Test.setText("ID passed is" + id);
break;
}
case 2:{
// Location 2
ImageButton ImageButton = (ImageButton)findViewById(R.id.imageButtonGhostCamLocation);
ImageButton .setBackgroundResource(R.drawable.image2);
ImageButton .setVisibility(0);
TextView Test = (TextView)findViewById(R.id.textView);
Test.setVisibility(0);
Test.setText("ID passed is" + id);
break;
更新
开始工作了!我刚刚从 xml 布局中的 ImageButton 中删除了 android src,它现在工作正常。感谢您的帮助!
I'm trying to update the image of an imageButton on run time. I have a switch statement that checks for an ID passed from another activity. I know the switch statement is working as the correct ID is passed to the TextView.
I've been searching and see some examples use the ImageView and others use the ImageButton. As you can see below I've tried both and none work.
XML Layout:
<ImageButton android:visibility="gone" android:id="@+id/imageButton" android:src="@drawable/defaultimage" android:layout_width="97dp" android:layout_height="95dp"></ImageButton>
<TextView android:visibility="gone" android:text="TextView" android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true"></TextView>
Java Code:
case 1:{
// Location 1
ImageView ImageButton = (ImageView)findViewById(R.id.imageButton);
ImageButton .setImageResource(R.drawable.image1);
ImageButton .setVisibility(0);
TextView Test = (TextView)findViewById(R.id.textView);
Test.setVisibility(0);
Test.setText("ID passed is" + id);
break;
}
case 2:{
// Location 2
ImageButton ImageButton = (ImageButton)findViewById(R.id.imageButtonGhostCamLocation);
ImageButton .setBackgroundResource(R.drawable.image2);
ImageButton .setVisibility(0);
TextView Test = (TextView)findViewById(R.id.textView);
Test.setVisibility(0);
Test.setText("ID passed is" + id);
break;
UPDATE
Got it to work! I just removed the android src from the ImageButton in xml layout and it's working fine now. Thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
替换此
您在 xml 中使用
ImageButton
并将其获取为 Java 中的ImageView
replace this
You are using
ImageButton
in xml and getting it asImageView
in Java您的代码的逻辑似乎是正确的(除非您的原始代码中有诸如
ImageButton ImageButton
之类的有趣的东西)。问题一定出在其他地方。如果从布局中删除
android:visibility="gone"
,您能看到该按钮吗?顺便说一句,而不是:
使用
这样它更具可读性。
The logic of your code seems correct(unless you have funny things like
ImageButton ImageButton
in your original code). The problem must be somewhere else.Can you see the button if you remove
android:visibility="gone"
from layout?Btw instead of:
use
That way it is more readable.
不允许使用像 ImageButton 这样的基本函数作为变量。
使用此代码
在 onCreate() 中
并在代码中使用此 myButton 变量。
will not allow to use basic functions like this ImageButton as a variable.
use this code
in onCreate()
and this myButton variable in the code.