android 手机主界面侧滑退出app问题

最近重新搭了个app,发现手机显示APP主界面时,沿着手机右边向左滑,会直接关闭应用,所以想搞个第一次提示,第二次退出app的效果。

结果搞出个复杂的东西,下面是两段代码。1:

1:GestureDetector+扩展函数。其实这个我觉得自己写的挺好,在一些不是系统级的滑动场景下应该有用。但时在返回退出这个需求上无法监听,实现效果。。。

Kotlin 复制代码
class SwipeGestureListener(private val context:Context,private val onSwipeLeft:()->Unit)
    :GestureDetector.SimpleOnGestureListener() {

    override fun onFling(
        e1: MotionEvent,
        e2: MotionEvent,
        velocityX: Float,
        velocityY: Float
    ): Boolean {
        Log.e("滑动","onFling")
        val deltaX =e2.x -e1.x
        val deltaY =e2.y -e1.y
        if (Math.abs(deltaX)>Math.abs(deltaY) && deltaX<0){
            onSwipeLeft.invoke()
            return true
        }
       return false
    }

//    override fun onScroll(
//        e1: MotionEvent,
//        e2: MotionEvent,
//        distanceX: Float,
//        distanceY: Float
//    ): Boolean {
//        Log.e("滑动","onScroll")
//        // 判断手势为从右到左的滑动,并且滑动距离较大
//        if (distanceX < 0 && Math.abs(distanceX) > Math.abs(distanceY)) {
//            onSwipeLeft.invoke()
//            return true
//        }
//
//        return false
//    }

}

/**
 * AppCompatActivity的扩展函数。
 */
fun AppCompatActivity.setupSwipeGesture(
    view: View,
    onSwipeLeft: () -> Unit
):GestureDetector{
    val gestureDetector =GestureDetector(this,SwipeGestureListener(this,onSwipeLeft))

    view.setOnTouchListener{_,event->

        gestureDetector.onTouchEvent(event)
    }
    return gestureDetector
}

理解这段代码需要考虑两个主要部分:SwipeGestureListener 类和 setupSwipeGesture 扩展函数。

首先,SwipeGestureListener 类是一个实现了 GestureDetector.SimpleOnGestureListener 的监听器,用于处理左滑手势。它有一个构造函数,接受一个 Context 和一个 lambda 表达式 onSwipeLeft: () -> Unit。在 onFling 方法中,我们检测手势为向左滑动,并在满足条件时调用传入的 onSwipeLeft lambda 表达式。

接下来,我们有一个扩展函数 setupSwipeGesture,它是一个在 AppCompatActivity 上定义的扩展函数。这个函数接受一个 lambda 表达式 onSwipeLeft: () -> Unit,并返回一个 GestureDetector 对象。在这个函数中,我们创建了一个 SwipeGestureListener 实例,并将传入的 onSwipeLeft lambda 表达式作为参数传递。然后,我们设置 onTouchListener,并在其中调用 gestureDetector.onTouchEvent(event),以处理触摸事件。

最后,在 MainActivity 中,我们在 onCreate 方法中调用了 setupSwipeGesture,并传入了一个处理左滑的 lambda 表达式 handleLeftSwipe。在 handleLeftSwipe 方法中,我们根据时间间隔判断是否显示退出提示。

Kotlin 复制代码
class MainActivity : AppCompatActivity() {

    private lateinit var gestureDetector: GestureDetector
    private var lastSwipeTime: Long = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        gestureDetector = setupSwipeGesture {
            handleLeftSwipe()
        }
    }

    private fun handleLeftSwipe() {
        val currentTime = System.currentTimeMillis()

        // 如果两次左滑的时间间隔小于2秒,且次数为1,显示提示
        if (currentTime - lastSwipeTime < 2000) {
            showExitToast()
            finish()
        }

        // 更新上一次左滑时间
        lastSwipeTime = currentTime
    }
}

总的来说,这段代码的原理是通过使用 GestureDetectorSwipeGestureListener 来监听左滑手势,然后在满足条件时执行特定的逻辑。这样的设计使得代码更加模块化和易于理解。

2.老办法onKeyDown,简单好用

Kotlin 复制代码
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK && event?.action == KeyEvent.ACTION_DOWN){
            if (System.currentTimeMillis() - lastSwipeTime > 2000) {

                lastSwipeTime = System.currentTimeMillis()
                "再按一次退出".showToast(this)
                Log.e("","再按一次退出")
            } else {
                finish()
            }
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

这个就不用解释了吧。

相关推荐
2501_9159090617 分钟前
Charles中文版使用教程 高效抓包与API调试常见问题处理
android·ios·小程序·https·uni-app·iphone·webview
智江鹏23 分钟前
Android 之 RxJava2
android
御水流红叶1 小时前
安卓加固脱壳
android·开发语言·python
智江鹏3 小时前
Android 之 网络通信(HTTP/TCP/UDP/JSON)
android
xq95273 小时前
android webview和 js 各种用法交互
android
北有花开4 小时前
Android方法耗时监控插件:基于ASM字节码插桩的性能分析工具
android
whysqwhw4 小时前
React Native应用中实现原生组件与JavaScript组件的复杂交互
android
whysqwhw4 小时前
React Native 中调用 Android 自定义控件
android
往事如烟隔多年5 小时前
一加Ace5无法连接ColorOS助手解决(安卓设备ADB模式无法连接)
android·adb·手机·coloros
00后程序员张5 小时前
iOS软件性能监控实战指南 开发到上线的完整流程解析
android·ios·小程序·https·uni-app·iphone·webview