1
我已經創建了一個應用程序,假設每次點擊都切換按鈕背景顏色。這裏是代碼:切換按鈕背景不起作用
package com.example.flash.light;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button screen = (Button) findViewById(R.id.screen);
Drawable background = getResources().getDrawable(R.drawable.black);
screen.setBackgroundDrawable(background);
screen.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
Button screen = (Button) findViewById (R.id.screen);
Drawable background = screen.getResources().getDrawable(screen.getId());
if(background == getResources().getDrawable(R.drawable.black)){
Drawable newBackgroun = getResources().getDrawable(R.drawable.white);
screen.setBackgroundDrawable(background);
}
if(background == getResources().getDrawable(R.drawable.white)){
Drawable newBackgroun = getResources().getDrawable(R.drawable.black);
screen.setBackgroundDrawable(background);
}
}
});
}
點擊該按鈕不響應。 感謝
所以如何解決? – vlio20
@VladIoffe更新回答 - 粘貼的固定代碼被剪掉。但它仍然不能解決我相信不同實例的問題。但你可以先嚐試一下。 –