[英]How can I change the Background drawable of my button with onclick in Android?
我有一個帶有可繪制XML背景的按鈕
android:background="@drawable/greenbut"
我想通過單擊按鈕將背景更改為我的@drawable/yellowbut
。
我用此代碼嘗試過,但似乎是完全錯誤的。 我不能使用setBackground Function
。 你能幫忙嗎..?
public void colorChanger(View v)
{
Button changeBut = (Button) findViewById(R.id.button2);
changeBut.setBackground(R.drawable.yellowbut);
changeBut.isClickable();
}
嘗試這個
public void colorChanger(View v)
{
Button changeBut = (Button) findViewById(R.id.button2);
changeBut.setBackgroundResource(R.drawable.yellowbut);
changeBut.isClickable();
}
就像遵循代碼一樣簡單,希望對您有所幫助
setContentView(R.layout.activity_main);
final Button changeBut = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button.setBackgroundColor(Color.yellow);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.