繁体   English   中英

Android java改变drawable图像

[英]Android java change drawable image

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

按下按钮时我要更改的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>

这是我按下按钮时我的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;

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

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

这应该工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM