我有我的應用程序一個簡單的按鈕。
我要做到以下幾點事情:
當應用程序運行,按鈕的顏色發生變化連續地(例如每3秒)沒有任何接觸或聚焦,趕上客戶的眼睛點擊它。
有沒有辦法做到這一點?改變Android的按鈕顏色不斷
回答
使用以下代碼:
Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run()
{
int rnd = (int)(Math.random() * 4);
if(rnd==0)
btn.setBackgroundColor(Color.BLUE);
if(rnd==1)
btn.setBackgroundColor(Color.RED);
if(rnd==2)
btn.setBackgroundColor(Color.GREEN);
if(rnd==3)
btn.setBackgroundColor(Color.YELLOW);
btn.invalidate();
handler.postDelayed(runnable, 3000);
}
};
handler.postDelayed(runnable, 3000);
什麼是invalidate(); ?日食不認識它 –
刪除它後嘗試。 –
無效()是用於刷新視圖 –
對於重複的顏色 -
Button btn = (Button) findViewById(R.id.btn);
Handler handler = new Handler();
final Runnable r = new Runnable() {
public void run() {
int i = 0;
if (i == 0) {
btn.setBackgroundColor(Color.YELLOW);
i++;
} else if (i == 1) {
btn.setBackgroundColor(Color.RED);
i++;
} else if (i == 2) {
btn.setBackgroundColor(Color.BLUE);
i++;
} else if (i == 3) {
btn.setBackgroundColor(Color.GREEN);
i = 0;
}
handler.postDelayed(this, 3000); // Set time in milliseconds
}
};
handler.postDelayed(r, 3000); // Set time in milliseconds
此代碼改變按鈕的顏色的順序每隔3秒 - 黃,紅,藍,綠。
隨機顏色 -
Button btn = (Button) findViewById(R.id.btn);
Handler handler = new Handler();
final Runnable r = new Runnable() {
public void run() {
int i = (int) Math.random() * 3;
if (i == 0) {
btn.setBackgroundColor(Color.YELLOW);
} else if (i == 1) {
btn.setBackgroundColor(Color.RED);
} else if (i == 2) {
btn.setBackgroundColor(Color.BLUE);
} else if (i == 3) {
btn.setBackgroundColor(Color.GREEN);
}
handler.postDelayed(this, 3000); // Set time in milliseconds
}
};
handler.postDelayed(r, 3000); // Set time in milliseconds
如果你喜歡這個答案,請把它標記爲selected
。
感謝,但顏色變爲藍色一次,然後它不更改爲其他顏色 –
@PouyaHeydari刪除'無效()'請 – FadedCoder
@PouyaHeydari試試我的新代碼,我更新了它。 – FadedCoder
在繪製的XML文件中聲明的動畫
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@drawable/frame1" android:duration="50" />
<item android:drawable="@drawable/frame2" android:duration="50" />
<item android:drawable="@drawable/frame3" android:duration="50" />
etc...
</animation-list>
,然後在代碼中你可以寫
imageView.setBackgroundResource(R.drawable.movie);
AnimationDrawable anim = (AnimationDrawable)
imageView.getBackground();
anim.start();
- 1. 按鈕顏色不改變
- 2. 改變按鈕顏色
- 3. Android按鈕顏色變化
- 4. 的Android按鈕的背景顏色改變按鈕的大小
- 5. 按鈕將不會改變顏色(HTML5)
- 6. 按鈕顏色不應改變
- 7. Silverlight - 按鈕不會改變顏色
- 8. html按鈕不會改變顏色
- 9. Android按鈕文字顏色不變
- 10. 按鈕顏色不變android studio 1.5.1
- 11. JQuery Mobile按鈕的顏色不會改變顏色
- 12. Android:不改變背景顏色的圓形按鈕角落
- 13. 按鈕顏色不會改變多個按鈕的切換
- 14. gridView中按鈕的第一個按鈕不會改變顏色
- 15. CKEditor文本顏色按鈕不改變文本顏色
- 16. 試圖改變按鈕的顏色onclick
- 17. 點擊按鈕改變ImageView的顏色?
- 18. IOS:改變按鈕的顏色
- 19. 改變按鈕的顏色太慢
- 20. 動態改變按鈕的顏色
- 21. 改變按鈕的顏色兌現CSS
- 22. 改變懸停的按鈕顏色
- 23. 如何在按下按鈕時改變按鈕的顏色
- 24. 按下按鈕時改變按鈕的顏色
- 25. 改變顏色之前刪除按鈕
- 26. 一個按鈕改變多種顏色
- 27. 點擊按鈕顏色沒有改變
- 28. 按鈕一半改變顏色
- 29. 設置背景顏色改變按鈕
- 30. 更改顏色的按鈕
哦,是的,但你嘗試過什麼? –
使用'Handler'或'runOnUi thread' – Rustam