XPopupWindow,對系統的PopupWindow進行進一步封裝和增強以便於使用。採用Kotlin語言,提供了許多額外的功能方法例如設置彈窗位置,調整彈窗動畫等等。android
XPopupWindowgit
使用Gradle:github
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
dependencies {
implementation 'com.github.XuDeveloper:XPopupWindow:1.0.1'
}
複製代碼
以建立一個登陸彈窗爲例:express
(略,含有一個帳號輸入框,一個密碼輸入框以及登陸按鈕,github有demo)apache
/** * 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)
}
複製代碼
微信 | 支付寶 |
---|---|
![]() |
![]() |
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.
複製代碼