我有我的應用程序一個簡單的按鈕。
我要做到以下幾點事情:
當應用程序運行,按鈕的顏色發生變化連續地(例如每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. Android按鈕顏色變化
- 2. html按鈕不會改變顏色
- 3. 按鈕將不會改變顏色(HTML5)
- 4. 按鈕顏色不應改變
- 5. JQuery Mobile按鈕的顏色不會改變顏色
- 6. Android:不改變背景顏色的圓形按鈕角落
- 7. gridView中按鈕的第一個按鈕不會改變顏色
- 8. CKEditor文本顏色按鈕不改變文本顏色
- 9. 改變按鈕的顏色兌現CSS
- 10. 動態改變按鈕的顏色
- 11. 改變按鈕的顏色太慢
- 12. 按下按鈕時改變按鈕的顏色
- 13. 按鈕一半改變顏色
- 14. 改變顏色之前刪除按鈕
- 15. 一個按鈕改變多種顏色
- 16. 設置背景顏色改變按鈕
- 17. 點擊按鈕顏色沒有改變
- 18. 更改顏色的按鈕
- 19. UWP - 改變後退按鈕顏色而不改變最小化/關閉按鈕顏色
- 20. 使用SharedPreferencers Android更改按鈕顏色?
- 21. Android按鈕動畫(顏色變化)
- 22. 按鈕顏色變化時不變
- 23. colorControlHighlight在按下時不會改變平面按鈕的顏色
- 24. 改變凸鈕的顏色?
- 25. android上的漸變按鈕顏色更改
- 26. 單擊按鈕時改變剩餘按鈕的顏色
- 27. 由於背景顏色而改變了按鈕顏色
- 28. 改變點擊顏色爲隨機顏色的文本顏色(按鈕)
- 29. 更改按鈕顏色onClick
- 30. 顏色按鈕C#Xamarin Android
哦,是的,但你嘗試過什麼? –
使用'Handler'或'runOnUi thread' – Rustam