android——自定义控件(编辑框)、悬浮窗

一、自定义编辑框

效果图:

主要的代码为:

class EditLayout @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr) {
    private var editTitle: String
    private var editContent: String
    private var editType: Int
    private var isMust: Boolean
    private var tvLabelEdit: TextView
    private var ivMustEdit: ImageView
    private var etEdit: EditText
    private var editable: Boolean

    init {
        LayoutInflater.from(context).inflate(R.layout.layout_edit, this)
        tvLabelEdit = findViewById(R.id.tv_label_edit)
        ivMustEdit = findViewById(R.id.iv_must_edit)
        etEdit = findViewById(R.id.et_edit)

        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.EditLayout)
        editTitle = typedArray.getString(R.styleable.EditLayout_editTitle) ?: ""
        editContent = typedArray.getString(R.styleable.EditLayout_editContent) ?: ""
        editType = typedArray.getInt(R.styleable.EditLayout_editType, 1)
        editable = typedArray.getBoolean(R.styleable.EditLayout_editable, true)
        isMust = typedArray.getBoolean(R.styleable.EditLayout_isMust, false)

        typedArray.recycle();
        applyLabel()
        applyIv()
        applyEdit()
    }

    private fun applyLabel() {
        tvLabelEdit.text = editTitle
        etEdit.setText(editContent)
    }

    private fun applyIv() {
        ivMustEdit.visibility = if (isMust) View.VISIBLE else View.GONE
    }

    private fun applyEdit() {
        etEdit.inputType = when (editType) {
            1 -> InputType.TYPE_CLASS_TEXT
            2 -> InputType.TYPE_CLASS_NUMBER
            else -> InputType.TYPE_CLASS_TEXT
        }
        etEdit.isEnabled = editable
    }

    fun getEditText(): EditText {
        return etEdit
    }

    fun getInputText(): String {
        return etEdit.text.toString()
    }

    fun setInputText(input: String) {
        etEdit.setText(input)
    }
}

使用3个原生控件组合而成,具体的代码可以到这里下载:

https://download.csdn.net/download/wy313622821/88467564

二、悬浮窗

主要的代码:

@SuppressLint("ClickableViewAccessibility")
class FloatWindow(private val mContext: Context) : LifecycleService() {
    private var floatRootView: View? = null
    private val mBinding: WindowFloatBinding by lazy {
        DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.window_float, null, false)
    }
    private val windowManager: WindowManager by lazy {
        mContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager
    }
    private val layoutParams: WindowManager.LayoutParams by lazy {
        WindowManager.LayoutParams().apply {
            width = WindowManager.LayoutParams.WRAP_CONTENT
            height = WindowManager.LayoutParams.WRAP_CONTENT
            flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
            gravity = Gravity.END

            type = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
            } else {
                WindowManager.LayoutParams.TYPE_PHONE
            }
        }
    }
    private var mAllDoorListen: () -> Unit = {}

    init {
        floatRootView = mBinding.root
        floatRootView?.setOnTouchListener(ItemViewTouchListener(layoutParams, windowManager))
        floatRootView?.setBackgroundColor(Color.TRANSPARENT)

        val outMetrics = DisplayMetrics()
        windowManager.defaultDisplay.getMetrics(outMetrics)

        layoutParams.format = PixelFormat.TRANSPARENT
        mBinding.tvOpenDoorAll.setOnClickListener {
        }
        mBinding.clLeft.setOnClickListener {
            Log.e("TAG", "缩小")
            indenteOrExpand()
        }

    }

    /** 缩进或者展开 **/
    private fun indenteOrExpand() {
        mBinding.llContentOperate.let {
            if (it.visibility == View.GONE) {
                it.visibility = View.VISIBLE
                mBinding.ivIndenteExpand.setImageResource(R.mipmap.ic_indentation_float)
            } else {
                it.visibility = View.GONE
                mBinding.ivIndenteExpand.setImageResource(R.mipmap.ic_expand_float)
            }
        }
    }

    inner class ItemViewTouchListener(
        private val wl: WindowManager.LayoutParams,
        private val windowManager: WindowManager
    ) : View.OnTouchListener {
        //        private var x = 0
        private var y = 0
        override fun onTouch(view: View, motionEvent: MotionEvent): Boolean {
            Log.e("TAG", "位置改变")
            when (motionEvent.action) {
                MotionEvent.ACTION_DOWN -> {
//                    x = motionEvent.rawX.toInt()
                    y = motionEvent.rawY.toInt()

                }
                MotionEvent.ACTION_MOVE -> {
//                    val nowX = motionEvent.rawX.toInt()
                    val nowY = motionEvent.rawY.toInt()
//                    val movedX = nowX - x
                    val movedY = nowY - y
//                    x = nowX
                    y = nowY
                    wl.apply {
//                        x += movedX
                        y += movedY
                    }
                    //更新悬浮球控件位置
                    windowManager.updateViewLayout(view, wl)
                }
                else -> {

                }
            }
            return false
        }
    }

    /** 悬浮窗显示 */
    fun show() {
        windowManager.addView(floatRootView, layoutParams)
    }

    /** 悬浮窗移除 */
    fun remove() {
        floatRootView?.let {
            windowManager.removeViewImmediate(it)
            floatRootView = null
        }
    }

    /** 设置全部开启按钮监听 */
    fun setOpenAllListener(mListen: () -> Unit) {
        mAllDoorListen = mListen
    }
}

详细的代码请到这里下载:https://download.csdn.net/download/wy313622821/88468147

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Android Studio中,您可以通过创建自定义视图类来实现自定义控件。下面是一些步骤,帮助您开始创建自己的自定义控件: 1. 创建一个新的Java类,该类将扩展现有的Android视图类(例如,TextView、Button等),或者直接扩展View类以创建一个全新的自定义视图。 2. 在类中实现构造函数和必要的方法。您可能需要重写onDraw()方法来处理绘制自定义视图的逻辑。还可以重写其他方法(例如,onMeasure()、onLayout()等),以便根据需要对自定义视图进行测量和布局。 3. 在XML布局文件中使用您的自定义控件。在布局文件中添加一个对应于您自定义控件类的标签,并根据需要设置属性。例如,如果您的自定义控件具有自定义属性,您可以在XML中设置这些属性。 4. 在Java代码中使用您的自定义控件。在Activity或Fragment中,通过findViewById()方法找到布局文件中的自定义控件,并使用它们。 5. 可选步骤:如果您的自定义控件需要处理用户交互事件(例如,点击事件),您可以重写相应的方法(例如,onTouchEvent())来实现所需的功能。 这只是一个简单的指南,让您了解如何在Android Studio中创建自定义控件。在实际开发过程中,您可能需要更多的步骤和代码来实现您的具体需求。您可以参考Android开发文档和其他教程,以获取更详细的指导和示例代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wy313622821

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值