java Android java更改可绘制图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7605294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:39:33  来源:igfitidea点击:

Android java change drawable image

javaandroid

提问by Hailei Edwards

Alright so how when i click a button? it will change the a image to a different image

好吧,当我点击一个按钮时如何?它会将图像更改为不同的图像

my xml of the image i want to change when button press:

我想在按下按钮时更改图像的 xml:

<ImageView
    android:id="@+id/bluebtn1" 
    android:src="@drawable/buttonblue" 
    android:layout_width="60dp" 
    android:layout_height="60dp"  
    android:layout_marginLeft="33dp"
    android:layout_marginTop="140dp"></ImageView>

and here is my java code ill be doing when you press the button

当你按下按钮时,这是我的 Java 代码

    case R.id.redbtn1:
            if (lvl1.getText().equals("1")) {
                lvl1.setText("2");

// Here is where the code goes to change the image

            } else {
                Toast.makeText(main.this, "YOU LOSE!", Toast.LENGTH_SHORT).show();
            }
        break;

回答by blessenm

Put this in your click listener.

把它放在你的点击监听器中。

ImageView blueBtn  = (ImageView)findViewById(R.id.bluebtn1);
blueBtn.setImageResource(R.drawable.YOUR_NEWIMAGE);

This should work.

这应该有效。