• 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

  • 相关阅读:
    驭数有道,天翼云数据库 TeleDB 全新升级
    避坑之路 —— 前后端 json 的注意问题
    设计模式(十一)----结构型模式之装饰者模式
    【交通建模】基于模型的自主交通仿真框架附matlab代码
    配置DDNS结合光猫路由器实现外网映射
    贝叶斯推理
    迪萧科技有限公司邀您参观2024生物发酵展
    XShelll-修改快捷键-xftp-修改编辑器
    【oracle删除表 回滚操作】
    【宠物用品】宠物饮水机方案
  • 原文地址:https://blog.csdn.net/wy313622821/article/details/134018665