簡體   English   中英

特定按鈕單擊后的操作

[英]Action after specific Button Clicks

我只想知道例如在單擊3個按鈕后如何啟動一個動作(例如啟動一個新的Activity)。 因此,只有單擊按鈕3次后,活動才會開始。

記下您在任意位置單擊“ Button ”的次數,例如全局變量,或者如果您希望它更簡潔,請單擊“ Button本身”。 向按鈕添加偵聽器,您只需將android:onClick="buttonClick"到xml中,然后實現具有該名稱的方法,如下所示:

public void buttonClick(View yourButton){
    if(yourButton.getTag() == null){ //We have no tags, so first click :)
        yourButton.setTag(1);
    }

    if((Integer)yourButton.getTag() == 3){
        //Do whatever
    }else{
        //Increment the value of the tag
        yourButton.setTag(((Integer)yourButton.getTag())+1);
    }
}

您可以維護一個計數器..每次單擊后將其遞增...檢查值是否等於所需值,然后建立意圖並啟動活動..在啟動活動之前重置計數器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM