自定義長按按鈕

import android.content.Context

import android.os.SystemClock

import android.util.AttributeSet

import android.view.MotionEvent

import android.widget.Button

import kotlinx.coroutines.*

import kotlinx.coroutines.flow.MutableSharedFlow

import kotlinx.coroutines.flow.collect

import kotlinx.coroutines.flow.flow

import kotlin.math.max

class RollBackButton @JvmOverloads constructor(

? ? context: Context,

? ? attrs: AttributeSet? = null,

? ? defStyleAttr: Int = 0

) : Button(context, attrs, defStyleAttr) {

? ? // 配置參數(shù)

? ? var longPressDuration = 500L // 默認(rèn)500毫秒

? ? var repeatInterval = 150L // 默認(rèn)150毫秒

? ? // 事件回調(diào)接口

? ? interface OnRollBackButtonListener {

? ? ? ? fun onLongPress()

? ? ? ? fun onShortPress()

? ? ? ? fun onRelease()

? ? }

? ? private var listener: OnRollBackButtonListener? = null

? ? private var job: Job? = null

? ? private val eventFlow = MutableSharedFlow<Event>()

? ? // 設(shè)置事件監(jiān)聽器

? ? fun setOnRollBackButtonListener(listener: OnRollBackButtonListener) {

? ? ? ? this.listener = listener

? ? ? ? // 啟動協(xié)程來收集事件

? ? ? ? CoroutineScope(Dispatchers.Main).launch {

? ? ? ? ? ? eventFlow.collect { event ->

? ? ? ? ? ? ? ? when (event) {

? ? ? ? ? ? ? ? ? ? is Event.LongPress -> listener?.onLongPress()

? ? ? ? ? ? ? ? ? ? is Event.ShortPress -> listener?.onShortPress()

? ? ? ? ? ? ? ? ? ? is Event.Release -> listener?.onRelease()

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }

? ? }

? ? private sealed class Event {

? ? ? ? object LongPress : Event()

? ? ? ? object ShortPress : Event()

? ? ? ? object Release : Event()

? ? }

? ? override fun onTouchEvent(event: MotionEvent): Boolean {

? ? ? ? when (event.action) {

? ? ? ? ? ? MotionEvent.ACTION_DOWN -> {

? ? ? ? ? ? ? ? job?.cancel() // 取消之前的長按任務(wù)

? ? ? ? ? ? ? ? job = CoroutineScope(Dispatchers.Main).launch {

? ? ? ? ? ? ? ? ? ? val startTime = SystemClock.elapsedRealtime()

? ? ? ? ? ? ? ? ? ? delay(longPressDuration)

? ? ? ? ? ? ? ? ? ? if (SystemClock.elapsedRealtime() - startTime >= longPressDuration) {

? ? ? ? ? ? ? ? ? ? ? ? // 長按事件

? ? ? ? ? ? ? ? ? ? ? ? eventFlow.emit(Event.LongPress)

? ? ? ? ? ? ? ? ? ? ? ? // 重復(fù)觸發(fā)長按事件

? ? ? ? ? ? ? ? ? ? ? ? while (SystemClock.elapsedRealtime() - startTime >= longPressDuration) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? delay(repeatInterval)

? ? ? ? ? ? ? ? ? ? ? ? ? ? eventFlow.emit(Event.LongPress)

? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? return true

? ? ? ? ? ? }

? ? ? ? ? ? MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {

? ? ? ? ? ? ? ? job?.cancel()

? ? ? ? ? ? ? ? val elapsedTime = SystemClock.elapsedRealtime() - event.downTime

? ? ? ? ? ? ? ? if (elapsedTime < longPressDuration) {

? ? ? ? ? ? ? ? ? ? eventFlow.emit(Event.ShortPress)

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? eventFlow.emit(Event.Release)

? ? ? ? ? ? ? ? return true

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? return super.onTouchEvent(event)

? ? }

}



class MainActivity : AppCompatActivity() {


? ? override fun onCreate(savedInstanceState: Bundle?) {

? ? ? ? super.onCreate(savedInstanceState)

? ? ? ? setContentView(R.layout.activity_main)


? ? ? ? val rollBackButton: RollBackButton = findViewById(R.id.roll_back_button)

? ? ? ? rollBackButton.setOnRollBackButtonListener(object : RollBackButton.OnRollBackButtonListener {

? ? ? ? ? ? override fun onLongPress() {

? ? ? ? ? ? ? ? // 處理長按事件

? ? ? ? ? ? ? ? Toast.makeText(this@MainActivity, "Long press detected", Toast.LENGTH_SHORT).show()

? ? ? ? ? ? }


? ? ? ? ? ? override fun onShortPress() {

? ? ? ? ? ? ? ? // 處理按下時(shí)間過短事件

? ? ? ? ? ? ? ? Toast.makeText(this@MainActivity, "Short press detected", Toast.LENGTH_SHORT).show()

? ? ? ? ? ? }


? ? ? ? ? ? override fun onRelease() {

? ? ? ? ? ? ? ? // 處理松手事件

? ? ? ? ? ? ? ? Toast.makeText(this@MainActivity, "Release detected", Toast.LENGTH_SHORT).show()

? ? ? ? ? ? }

? ? ? ? })

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容