【问题标题】:Having trouble changing image on imageButton at run time在运行时更改 imageButton 上的图像时遇到问题
【发布时间】: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


【解决方案1】:

替换这个

ImageButton imageButton = (ImageButton)findViewById(R.id.imageButton);
imageButton .setImageResource(R.drawable.image1);
imageButton .setVisibility(0);

您在 xml 中使用 ImageButton 并在 Java 中将其获取为 ImageView

【讨论】:

  • 如前所述,我将两者都包含在这里是为了展示我所做的事情。两个开关都不能正常工作。如果我使用 ImageButton 或 ImageView(正如我在各个地方看到的那样)。
【解决方案2】:

您的代码逻辑似乎是正确的(除非您的原始代码中有 ImageButton ImageButton 之类的有趣内容)。问题一定出在其他地方。

如果你从布局中删除android:visibility="gone",你能看到按钮吗?

顺便说一句,而不是:

setVisibility(0);

使用

setVisibility(View.VISIBLE);

这样更易读。

【讨论】:

  • 感谢您的提示!我在找这个。 :-)
【解决方案3】:

不允许使用像这个 ImageButton 这样的基本函数作为变量。 使用此代码

private ImageButton myButton;

在 onCreate() 中

myButton = (ImageButton) findViewById(R.id.imageButton);

以及代码中的这个 myButton 变量。

case 1:{
        // Location 1            
        myButton .setImageResource(R.drawable.image1);
        my .setVisibility(0);
        TextView Test = (TextView)findViewById(R.id.textView);
        Test.setVisibility(0);
        Test.setText("ID passed is" + id);
        break;
        }
    case 2:{
        // Location 2          
        myButton.setBackgroundResource(R.drawable.image2);
        my.setVisibility(0);
        TextView Test = (TextView)findViewById(R.id.textView);
        Test.setVisibility(0);
        Test.setText("ID passed is" + id);
        break;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-23
    • 2011-12-30
    • 2010-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多