0
愚蠢noob問題,但希望有人善意幫助! 我試圖在按下按鈕時更改兩個源之間的圖像。兩個圖像存儲在可繪製的pic1和pic2中。但運行代碼會導致圖片更改兩次,然後不再進一步更改。有人可以解釋嗎?爲什麼android圖像兩次後停止更改?
下面是代碼:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button changeBtn = (Button) findViewById(R.id.buttonChange);
final ImageView image = (ImageView) findViewById(R.id.image1);
final Drawable current = image.getDrawable(); //this is pic1
changeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(image.getDrawable()==current) //if pic1
image.setImageResource(R.drawable.pic2);
else image.setImageResource(R.drawable.pic1);
}
});