【发布时间】:2011-10-20 11:36:34
【问题描述】:
我正在尝试在运行时更新 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,它现在工作正常。感谢您的帮助!
【问题讨论】:
-
首先,您应该检查 Java 命名约定 (java.about.com/od/javasyntax/a/nameconventions.htm)。
ImageButton ImageButton = (ImageButton)findViewById(..)没有任何意义。 -
我在这里更改了名称以使其更易于阅读... ImageButton ImageButton 实际上是 ImageButton myImageButtonName。对于那个很抱歉。 :-)
标签: android imagebutton