kotlin 自定义popwindow

2018-12-07  本文已影响0人  weiminsir
Android kotlin 自定义 popwindow
class RecordingPopWindow : PopupWindow {

    private var recordingView: View? = null
    private var voiceView: TextView? = null
    private var cancelView: TextView? = null
    private var sendView: TextView? = null
    private var text = ""


    constructor(ctx: Context) : super(ctx) {
        init(ctx, text = "")
    }

    constructor(ctx: Context, text: String) : super(ctx) {
        init(ctx, text)
        this.text = text
    }

    private var callBack: CallBack? = null

    private fun init(context: Context, text: String) {
        val inflater: LayoutInflater = LayoutInflater.from(context)
        recordingView = inflater.inflate(R.layout.dialog_recording, null)
        this.contentView = recordingView
        this.width = LinearLayout.LayoutParams.MATCH_PARENT
        this.height = LinearLayout.LayoutParams.WRAP_CONTENT
        this.isTouchable = true
        this.isFocusable = false
        this.isOutsideTouchable = false
        this.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.bg_dialog_record_r1))
        this.animationStyle = android.R.style.Animation_Dialog
        voiceView = recordingView?.find<TextView>(R.id.tv_record_content)
        cancelView = recordingView?.find<TextView>(R.id.tv_record_cancel)
        sendView = recordingView?.find<TextView>(R.id.tv_record_send)
        voiceView?.text = text
        //确认和点击text 参数回传
        voiceView?.setOnClickListener {
            dismiss()
            callBack?.onShowContent(voiceView?.text.toString().trim())
        }
        sendView?.setOnClickListener {
            callBack?.onSendContent(this.text)
            dismiss()
        }
        cancelView?.setOnClickListener {
            dismiss()
        }
    }

    fun setCallBack(callBack: CallBack) {
        this.callBack = callBack
    }

    fun setText(text: String) {
        this.text = text
        voiceView?.text = text
    }

    /**
     * 显示popupWindow
     *
     * @param parent
     */
    fun showPopupWindow(parent: View) {
        if (!this.isShowing) {
            this.showAtLocation(parent, Gravity.BOTTOM, 0, 300)
        }
    }


    interface CallBack {
        fun onShowContent(content: String)
        fun onSendContent(content: String)
    }

}

Fragment 中初始化

private fun initPopWindow() {
        recordingWindow = RecordingPopWindow(activity!!)
        recordingWindow?.setText("123456")
        recordingWindow?.setCallBack(object : RecordingPopWindow.CallBack {
            override fun onShowContent(content: String) {
                
            }

            override fun onSendContent(content: String) {
                
            }

        })

    }

添加单击事件

      if (recordingWindow == null) {
            initPopWindow()
          }
   //  //这里设置宽度 否则非正常显示  构造方法设置定值默认无效 必须popwindow 初始化之后 设置才有效
        recordingWindow?.setText("测试显示text")
        val width = activity?.windowManager?.defaultDisplay?.width
        recordingWindow?.width = width!! - DensityUtil.dip2px(context, 24f)
//showPopupWindow  不能直接放在initview 中
        recordingWindow?.showPopupWindow(view)

XML 布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="@dimen/layout_dp_128"
    android:layout_marginLeft="@dimen/layout_dp_12"
    android:layout_marginRight="@dimen/layout_dp_12"
    android:background="@drawable/bg_r3"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_record_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="@dimen/layout_dp_12"
        android:text="老师演讲很精彩"
        android:textColor="@color/white"
        android:textSize="@dimen/text_size_15" />


    <ImageView
        android:id="@+id/iv_record_progress"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:layout_marginLeft="@dimen/layout_dp_12"
        android:layout_marginTop="@dimen/layout_dp_8"
        android:layout_marginRight="@dimen/layout_dp_12"
        android:layout_weight="1"
        android:background="@color/common_yellow"
        app:layout_constraintTop_toBottomOf="@+id/tv_record_content" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/brownGrey" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_record_cancel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="@dimen/layout_dp_12"
            android:text="取消"
            android:textColor="@color/white"
            android:textSize="@dimen/text_size_17" />

        <TextView
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@color/brownGrey" />

        <TextView
            android:id="@+id/tv_record_send"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="@dimen/layout_dp_12"
            android:text="发送"
            android:textColor="@color/white"
            android:textSize="@dimen/text_size_17"
            app:layout_constraintHorizontal_bias="1" />
    </LinearLayout>
</LinearLayout>
上一篇下一篇

猜你喜欢

热点阅读