[英]Setting Actor To Listen For Button Click In LibGDX
背景信息:我目前在為我的游戲擴展的Dialog類中工作。 在此對話框的內容表中,我既有圖像又有表(我們稱其為ioTable
)。 在ioTable
內部,我同時具有Labels和TextFields的組合。 這個想法是對話框變成一種供填寫的形式。
接下來,在對話框的按鈕表中,我想包含一個“清除” TextButton( clearButton
)。 clearButton
會清除所有寫入ioTable
的TextField的值的ioTable
。
我的問題:可以向ioTable
每個TextField中添加一個偵聽器,該偵聽器在按下clearButton
時將觸發。 與往常一樣,任何其他創意解決方案都倍受歡迎。
您可以只給EventListener引用要清除的表:
// Assuming getSkin() and ioTable are defined elsewhere and ioTable is final
TextButton clearButton = new TextButton("Clear", getSkin());
clearButton.addListener(new EventListener() {
@Override
public boolean handle(Event event) {
for(Actor potentialField : table.getChildren()) {
if(potentialField instanceof TextField) {
((TextField)potentialField).setText("");
}
}
return true;
}
});
// Add clearButton to your dialog
如果您看到自己創建了多個clearButtons,則可以輕松地將其包裝在輔助方法中或擴展TextButton
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.