易用版Popupwindow by Kotlin瞭解一下

張朝旭發表於2018-08-08

概述

XPopupWindow,對系統的PopupWindow進行進一步封裝和加強以便於使用。採用Kotlin語言,提供了許多額外的功能方法例如設定彈窗位置,調整彈窗動畫等等。

專案地址

XPopupWindow

預覽

XPopupWindow-demo

特性

  • 簡單快速地建立一個自定義彈窗
  • 以一種相對便捷的方式設定彈窗位置
  • 更加自由地調整你的彈窗動畫

開始

使用Gradle:

allprojects {
    repositories {
        ...
	maven { url "https://jitpack.io" }
    }
}

dependencies {
    implementation 'com.github.XuDeveloper:XPopupWindow:1.0.1'
}
複製程式碼

使用

以建立一個登入彈窗為例:

介面編寫

(略,含有一個賬號輸入框,一個密碼輸入框以及登入按鈕,github有demo)

建立XPopupWindow

/**
 * Created by Xu on 2018/6/17.
 * @author Xu
 */

class InputPopupWindow : XPopupWindow {
    private var btnLogin: Button? = null
    private var etPhone: TextInputEditText? = null

    constructor(ctx: Context) : super(ctx)

    constructor(ctx: Context, w: Int, h: Int) : super(ctx, w, h)

    /**
     * 設定popupwindow的layoutId
     */
    override fun getLayoutId(): Int {
        return R.layout.popup_input
    }

    /**
     * 設定layout的parentNodeId
     */
    override fun getLayoutParentNodeId(): Int {
        return R.id.input_parent
    }

    /**
     * 初始化介面
     */
    override fun initViews() {
        btnLogin = findViewById(R.id.btn_login)
        btnLogin?.setOnClickListener { dismiss() }
        etPhone = findViewById(R.id.et_mobile)
    }

    /**
     * 初始化資料
     */
    override fun initData() {
        // 設定彈窗背景透明度
        setShowingBackgroundAlpha(0.4f)
        // 彈窗彈出時自動獲取輸入框的焦點
        setAutoShowInput(etPhone, true)
    }

    /**
     * 為彈窗設定彈出動畫,如果不想設定或是想通過xml方式設定,則設定返回值為-1
     */
    override fun startAnim(view: View): Animator? {
        var animatorX: ObjectAnimator = ObjectAnimator.ofFloat(view, "scaleX", 0f, 1f)
        var animatorY: ObjectAnimator = ObjectAnimator.ofFloat(view, "scaleY", 0f, 1f)
        var set = AnimatorSet()
        set.play(animatorX).with(animatorY)
        set.duration = 500
        return set
    }

    /**
     * 為彈窗設定退出動畫,如果不想設定或是想通過xml方式設定,則設定返回值為-1
     */
    override fun exitAnim(view: View): Animator? {
        var animatorX: ObjectAnimator = ObjectAnimator.ofFloat(view, "scaleX", 1f, 0f)
        var animatorY: ObjectAnimator = ObjectAnimator.ofFloat(view, "scaleY", 1f, 0f)
        var set = AnimatorSet()
        set.play(animatorX).with(animatorY)
        set.duration = 700
        return set
    }

    /**
     * 通過xml方式設定動畫,xml編寫方法與原生popupwindow設定動畫方法相同
     */
    override fun animStyle(): Int {
        return -1
    }

}

複製程式碼

具體使用

private fun showInputPopup() {
    inputPopupWindow = InputPopupWindow(this, 1000, 600)
    // 可設定彈窗退出的監聽器,在回撥中執行相應操作
    inputPopupWindow?.setXPopupDismissListener(object : XPopupWindowDismissListener {
        override fun xPopupBeforeDismiss() {
        }

        override fun xPopupAfterDismiss() {
            Snackbar.make(findViewById(android.R.id.content), "登入成功!", Snackbar.LENGTH_LONG).show()
        }
    })
    inputPopupWindow?.showPopupFromScreenCenter(R.layout.activity_main)
}
複製程式碼
  • 你可以檢視xpopupwindowdemo以獲取更多使用方法!

給我買杯檸檬茶唄 :smile:

微信 支付寶
易用版Popupwindow by Kotlin瞭解一下
易用版Popupwindow by Kotlin瞭解一下

協議

Copyright [2018] XuDeveloper

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
複製程式碼

相關文章