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


    一、自定义编辑框

    效果图:

    主要的代码为:

    1. class EditLayout @JvmOverloads constructor(
    2. context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
    3. ) : ConstraintLayout(context, attrs, defStyleAttr) {
    4. private var editTitle: String
    5. private var editContent: String
    6. private var editType: Int
    7. private var isMust: Boolean
    8. private var tvLabelEdit: TextView
    9. private var ivMustEdit: ImageView
    10. private var etEdit: EditText
    11. private var editable: Boolean
    12. init {
    13. LayoutInflater.from(context).inflate(R.layout.layout_edit, this)
    14. tvLabelEdit = findViewById(R.id.tv_label_edit)
    15. ivMustEdit = findViewById(R.id.iv_must_edit)
    16. etEdit = findViewById(R.id.et_edit)
    17. val typedArray = context.obtainStyledAttributes(attrs, R.styleable.EditLayout)
    18. editTitle = typedArray.getString(R.styleable.EditLayout_editTitle) ?: ""
    19. editContent = typedArray.getString(R.styleable.EditLayout_editContent) ?: ""
    20. editType = typedArray.getInt(R.styleable.EditLayout_editType, 1)
    21. editable = typedArray.getBoolean(R.styleable.EditLayout_editable, true)
    22. isMust = typedArray.getBoolean(R.styleable.EditLayout_isMust, false)
    23. typedArray.recycle();
    24. applyLabel()
    25. applyIv()
    26. applyEdit()
    27. }
    28. private fun applyLabel() {
    29. tvLabelEdit.text = editTitle
    30. etEdit.setText(editContent)
    31. }
    32. private fun applyIv() {
    33. ivMustEdit.visibility = if (isMust) View.VISIBLE else View.GONE
    34. }
    35. private fun applyEdit() {
    36. etEdit.inputType = when (editType) {
    37. 1 -> InputType.TYPE_CLASS_TEXT
    38. 2 -> InputType.TYPE_CLASS_NUMBER
    39. else -> InputType.TYPE_CLASS_TEXT
    40. }
    41. etEdit.isEnabled = editable
    42. }
    43. fun getEditText(): EditText {
    44. return etEdit
    45. }
    46. fun getInputText(): String {
    47. return etEdit.text.toString()
    48. }
    49. fun setInputText(input: String) {
    50. etEdit.setText(input)
    51. }
    52. }

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

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

    二、悬浮窗

    主要的代码:

    1. @SuppressLint("ClickableViewAccessibility")
    2. class FloatWindow(private val mContext: Context) : LifecycleService() {
    3. private var floatRootView: View? = null
    4. private val mBinding: WindowFloatBinding by lazy {
    5. DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.window_float, null, false)
    6. }
    7. private val windowManager: WindowManager by lazy {
    8. mContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager
    9. }
    10. private val layoutParams: WindowManager.LayoutParams by lazy {
    11. WindowManager.LayoutParams().apply {
    12. width = WindowManager.LayoutParams.WRAP_CONTENT
    13. height = WindowManager.LayoutParams.WRAP_CONTENT
    14. flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or
    15. WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
    16. gravity = Gravity.END
    17. type = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    18. WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
    19. } else {
    20. WindowManager.LayoutParams.TYPE_PHONE
    21. }
    22. }
    23. }
    24. private var mAllDoorListen: () -> Unit = {}
    25. init {
    26. floatRootView = mBinding.root
    27. floatRootView?.setOnTouchListener(ItemViewTouchListener(layoutParams, windowManager))
    28. floatRootView?.setBackgroundColor(Color.TRANSPARENT)
    29. val outMetrics = DisplayMetrics()
    30. windowManager.defaultDisplay.getMetrics(outMetrics)
    31. layoutParams.format = PixelFormat.TRANSPARENT
    32. mBinding.tvOpenDoorAll.setOnClickListener {
    33. }
    34. mBinding.clLeft.setOnClickListener {
    35. Log.e("TAG", "缩小")
    36. indenteOrExpand()
    37. }
    38. }
    39. /** 缩进或者展开 **/
    40. private fun indenteOrExpand() {
    41. mBinding.llContentOperate.let {
    42. if (it.visibility == View.GONE) {
    43. it.visibility = View.VISIBLE
    44. mBinding.ivIndenteExpand.setImageResource(R.mipmap.ic_indentation_float)
    45. } else {
    46. it.visibility = View.GONE
    47. mBinding.ivIndenteExpand.setImageResource(R.mipmap.ic_expand_float)
    48. }
    49. }
    50. }
    51. inner class ItemViewTouchListener(
    52. private val wl: WindowManager.LayoutParams,
    53. private val windowManager: WindowManager
    54. ) : View.OnTouchListener {
    55. // private var x = 0
    56. private var y = 0
    57. override fun onTouch(view: View, motionEvent: MotionEvent): Boolean {
    58. Log.e("TAG", "位置改变")
    59. when (motionEvent.action) {
    60. MotionEvent.ACTION_DOWN -> {
    61. // x = motionEvent.rawX.toInt()
    62. y = motionEvent.rawY.toInt()
    63. }
    64. MotionEvent.ACTION_MOVE -> {
    65. // val nowX = motionEvent.rawX.toInt()
    66. val nowY = motionEvent.rawY.toInt()
    67. // val movedX = nowX - x
    68. val movedY = nowY - y
    69. // x = nowX
    70. y = nowY
    71. wl.apply {
    72. // x += movedX
    73. y += movedY
    74. }
    75. //更新悬浮球控件位置
    76. windowManager.updateViewLayout(view, wl)
    77. }
    78. else -> {
    79. }
    80. }
    81. return false
    82. }
    83. }
    84. /** 悬浮窗显示 */
    85. fun show() {
    86. windowManager.addView(floatRootView, layoutParams)
    87. }
    88. /** 悬浮窗移除 */
    89. fun remove() {
    90. floatRootView?.let {
    91. windowManager.removeViewImmediate(it)
    92. floatRootView = null
    93. }
    94. }
    95. /** 设置全部开启按钮监听 */
    96. fun setOpenAllListener(mListen: () -> Unit) {
    97. mAllDoorListen = mListen
    98. }
    99. }

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

  • 相关阅读:
    试试这2个流动图片制作方法让你的图片动起来吧
    【Django-meeting系统】Racher部署教室管理系统的方法(一个pod部署应用、redis、celery)---20221019
    什么是API网关?为什么要用API网关?
    谈谈期货程序化交易策略的客观性
    javascript阻止右键默认行为,重新添加右键新菜单
    ipv4 网络划分,网络段与子网掩码
    【数据结构】循环链表的增删查改
    机器学习第六课--朴素贝叶斯
    Python进行excel处理-01
    ArrayDeque详解(含动画演示)
  • 原文地址:https://blog.csdn.net/wy313622821/article/details/134018665