[英]How to change background of a button in android and retain it after few seconds
我提到了這個 --> https://stackoverflow.com/a/31367723/12553303
我嘗試了上述解決方案,但它不起作用 --> 幾秒鍾內未顯示可繪制對象
這是我的代碼:
buynow.setOnClickListener(object : View.OnClickListener{
override fun onClick(v: View?) {
// set the color red first.
buynow.setBackgroundResource(R.drawable.mybuttonred)
// change to original after 5 secs.
Handler().postDelayed(Runnable { buynow.setBackgroundResource(R.drawable.mybutton)
Toast.makeText(applicationContext,"ksjdf",Toast.LENGTH_LONG).show()
},
5000)
}
})
甚至吐司在點擊時也不起作用
我錯過了什么?
這對我來說很好用,您需要使視圖最終化,以便您可以在處理程序主體中訪問它
buyNow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
view.setBackgroundColor(Color.RED); //set the color to red
// Delay of 2 seconds (200 ms) before changing back the color to black
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
view.setBackgroundColor(Color.BLACK); //set the color to black
}
}, 200);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.