簡體   English   中英

Button android studio,如何創建一個停止 setonclicklistener 功能的按鈕?

[英]Button android studio, how to create a button that stops setonclicklistener function?

我有一個按鈕,例如

                <com.google.android.material.button.MaterialButton
                    android:id="@+id/button"
                    style="@style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
                    android:layout_width="150dp"
                    android:layout_height="50dp"
                    android:text="@string/lightning"
                    android:textColor="#FFFFFF"
                    android:textSize="14sp"
                    app:icon="@drawable/ic_baseline_flash_on_24"
                    app:iconTint="@color/white"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.061"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintVertical_bias="0.239"
                    app:strokeColor="@color/white"
                    app:strokeWidth="1dp" />

我創建了一個 setOnClickListener 方法,並且在它們內部是一個循環。

        val btnClickMe1 = findViewById<Button>(R.id.button)
        btnClickMe1.setOnClickListener {
                
               //do something here 20 times
        }

此方法工作正常,但是,我創建了一個 .apk 文件。 當我單擊按鈕時,功能運行。 我想知道如何通過單擊相同的按鈕退出循環。 不管循環是否結束。

創建一個變量,讓您知道操作的狀態,執行或不執行,如果變量正在執行,則執行所述操作。

當您按下按鈕時,變量將變為負數,當它為假時,循環中的下一次迭代將檢測它是否仍然處於活動狀態,否則將跳過循環。

private val isButtonLoopActive: Boolean = false

btn.setOnClickListener {
    isButtonLoopActive = !isButtonLoopActive 
    for (i in 1..20) {
        if (isButtonLoopActive) {
            //TODO
        } else {
            break
        }
    }
    isButtonLoopActive = false
}

你還沒有發布你的實際代碼是什么,但是如果你的循環只是在點擊監聽器中運行,那么執行將不會完成(並且線程將無法做任何其他事情,包括響應按鈕點擊)直到那個循環已完成,您退出偵聽器功能。

您需要設置以某種方式異步運行的循環(延遲的Runnable事件、協程、工作線程),以便它可以在不阻塞線程的情況下完成它的工作,並且應用程序的其余部分可以正常運行。 然后您的點擊偵聽器需要檢查該任務當前是否正在運行 - 如果沒有,則啟動它,如果是,則停止它。

這是您可以使用協程執行此操作的一種方法:

// a reference to the currently running task (if any)
private var job: Job? = null

// kick off a new task, and store a reference to it
private fun startTask() {
    // you'll have to use the appropriate scope and 'launchWhen' function
    // to keep it running/paused the way you want - depends what you're doing
    job = viewLifecycleOwner.lifecycleScope.launchWhenCreated {
        repeat(20) {
            // the actual thing you're doing!
            binding.text.isVisible = !binding.text.isVisible
            delay(500)
        }
    }
}

// stop any running task and discard it, so the next click will start a new one
private fun endTask() {
    job?.cancel()
    job = null
}

那么你的點擊監聽器可以是:

binding.button.setOnClickListener {
    // check if there's a running job -
    // if so, the click means stop, otherwise it means start
    if (job?.isActive == true) endTask() else startTask()
}

這只是一種方式——另一種典型的方式是有一個Runnable ,它在HandlerView上調用postDelayed ,因此它執行一個操作,然后發布自己以在將來再次運行(或者如果滿足某些已完成的條件,則不執行)。 您需要在某處有一些running布爾值,並使用它來查看發布是否應該開始,或者Runnable是否應該在下次運行時放棄。 當應用程序進入后台或其他任何情況時,它需要停止 - 再次,取決於你在做什么

嘗試這個

for (i in 1..20){
        Log.d("TAG", "Currently : $i")
    }

暫無
暫無

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

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