在运行时更改 imageButton 上的图像时遇到问题

发布于 2024-12-11 03:48:20 字数 1627 浏览 1 评论 0原文

我正在尝试在运行时更新 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吾性傲以野 2024-12-18 03:48:20

替换此

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

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

replace this

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

You are using ImageButton in xml and getting it as ImageView in Java

蹲墙角沉默 2024-12-18 03:48:20

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

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

顺便说一句,而不是:

setVisibility(0);

使用

setVisibility(View.VISIBLE);

这样它更具可读性。

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:

setVisibility(0);

use

setVisibility(View.VISIBLE);

That way it is more readable.

稀香 2024-12-18 03:48:20

不允许使用像 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;

will not allow to use basic functions like this ImageButton as a variable.
use this code

private ImageButton myButton;

in onCreate()

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

and this myButton variable in the code.

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文