我的表單中有五個按鈕,名稱爲buttonDo,buttonStop,buttonPause,buttonPlay,buttonselect。如何在for循環中選擇所有按鈕以檢查某些情況。我需要一個代碼,它可以找到完全沒有按鈕的代碼,並自行增加,直到找到最後一個按鈕。 Plz幫助我。我的表單中的所有按鈕使用for循環 - eclipse for android
-2
A
回答
2
嘗試與該代碼,第一個函數開始迭代,第二(第一被叫)穿行於景三和查找按鈕
public ArrayList<Button> getButtons() {
ArrayList<Button> buttons = new ArrayList<Button>();
ViewGroup viewGroup = (ViewGroup) getWindow().getDecorView();
findButtons(viewGroup, buttons);
return buttons;
}
private static void findButtons(ViewGroup viewGroup,ArrayList<Button> buttons) {
for (int i = 0, N = viewGroup.getChildCount(); i < N; i++) {
View child = viewGroup.getChildAt(i);
if (child instanceof ViewGroup) {
findButtons((ViewGroup) child, buttons);
} else if (child instanceof Button) {
buttons.add((Button) child);
}
}
}
希望能夠幫助所有可能的情況下!
1
在這種情況下,ll是一個LinearLayout,並且此搜索僅在此一個佈局中,並且不會遞歸搜索整個視圖樹。像這樣的東西可能有效。沒有測試。
Vector<Button> buttons = new Vector<Button>();
for (int i=0; i < ll.getChildCount(); i++){
View v = ll.getChildAt(i);
if (v instanceof Button) {
buttons.add(v);
}
}
Button lastButton = buttons.lastElement();
我不得不說這聽起來像是你可能以錯誤的方式接近問題。這聽起來像一個非常奇怪的方法。出於好奇,你想達到什麼目的?
相關問題
- 1. JavaScript - for循環中的循環按鈕
- 2. 我想使用For循環顯示錶中的所有記錄
- 3. 如何運行循環(例如for循環)按鈕單擊android
- 4. Android的 - 使用for循環
- 5. jQuery For循環單選按鈕
- 6. For循環中的停止按鈕
- 7. 爲什麼python for循環使用我列表的所有值?
- 8. 找了很多使用for循環(Android Studio中)按鈕
- 9. android在Java中使用for循環迭代按鈕id
- 10. 按鈕上的標籤增量單擊使用For循環C#
- 11. 的Android For循環
- 12. 使用數組for循環for循環
- 13. For循環在Javascript中的for循環
- 14. For循環所有可用的公司?
- 15. 如何用for循環制作按鈕
- 16. for循環單
- 17. PHP中的按鈕列表「For循環」不起作用
- 18. 如何更改Java中for循環中按鈕的名稱Android
- 19. for循環使用for循環中聲明的變量值?
- 20. 如何在此腳本的for循環中使用for循環?
- 21. (Python)使用for循環打包tkinter中的按鈕?
- 22. 從表中的for循環
- 23. for循環for循環?
- 24. for循環for循環? - Javascript
- 25. 在Java中使用Var for For循環
- 26. 在PHP中使用$ _GET for for循環
- 27. Android調用php for for循環
- 28. 使用「for循環」
- 29. 使用for循環
- 30. 使用for循環
你能幫我解決這個問題嗎 – 2014-08-30 07:16:27