• 自定义数字键盘(kotlin)


    最近做一个数字键盘的自定义view
    大概思路就是弹窗里面有个0-9的数字键盘(通过recycleview实现),点击可以展示选择的数字
    代码如下:
    遇到问题记录一下:
    字符串拼接:inputcontent += “$content”
    如果想要将pop里面的数据传到activity,最好就在当前activity里面展示pop
    1. implementation 'com.github.li-xiaojun:XPopup:2.9.0'
    2. implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.46'
    2.
    class PopupWindowActivity : AppCompatActivity() {
    private lateinit var binding: ActivityPopupWindowBinding
    lateinit var popWindow : BasePopupView
    lateinit var textView: TextView
    lateinit var etContent : String
    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityPopupWindowBinding.inflate(layoutInflater)
    setContentView(binding.root)
    initEvent()

    }
    
    private fun initEvent() {
        textView =  binding.tvOpen
        textView.setOnClickListener {
            popWindow = XPopup.Builder(this)
                .autoFocusEditText(false)
                .autoOpenSoftInput(false)
                .dismissOnTouchOutside(true)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    // .watchView()
    .asCustom(object : CenterPopupView(this){

                    lateinit var keyList : MutableList
                    lateinit var adpter:KeyBoardAdpter
                    lateinit var edittext: EditText
                    lateinit var tvClosePop:TextView
                    lateinit var gridLayoutManager: GridLayoutManager
                    override fun getImplLayoutId(): Int   = R.layout.custom_popup
                    override fun onCreate() {
                        super.onCreate()
                        initView()
                        initData()
                        initEventChild()
    
                    }
    
                    private fun initEventChild() {
                        tvClosePop.setOnClickListener {
                            popWindow.dismiss()
                            textView.text = etContent
                        }
                        gridLayoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup(){
                            override fun getSpanSize(position: Int): Int {
                                return if (position !== 10) 1 else 2
                            }
    
                        }
    
                    }
    
                    private fun initData() {
                        //获取资源文件数据
                        keyList = ArrayList()
                        val keyboards = context.resources.getStringArray(R.array.keyboard)
                        for (ele in keyboards){
                            keyList.add(ele)
                        }
                        adpter.addData(keyList)
    
                    }
    
                    private fun initView() {
                        val rvKeyboardNumber = findViewById(R.id.rv_keyboard_number)
    
                        tvClosePop = findViewById(R.id.tv_close_pop)
                        var inputcontent: String = "" //字符串拼接使用
                         gridLayoutManager = GridLayoutManager(context,3)
                        rvKeyboardNumber.layoutManager = gridLayoutManager
                        adpter = KeyBoardAdpter(R.layout.item_keyboard_key)
                        rvKeyboardNumber.adapter = adpter
                        edittext = findViewById(R.id.et_show_input)
                        adpter.setOnItemChildClickListener { adapter, view, position ->
                            if (position <10){
                                val content = (view as TextView).text
                                 inputcontent += "$content"
                                edittext.setText(inputcontent)
                                etContent = inputcontent
                            }else{
                                inputcontent = inputcontent.dropLast(1)
                                edittext.setText(inputcontent)
                                etContent = inputcontent
                            }
    
                        }
    
                    }
    
    
                }).show()
        }
    
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71

    }

  • 相关阅读:
    Kotlin只截取Float小数点后数值DecimalFormat
    linux-磁盘应用
    Word控件Spire.Doc 【文本】教程(20) ;如何在 C#、VB.NET 下检索 Word 中所有 TextRanges 的样式名称
    Docker(六)——挂载实现同步
    ElasticSearch ( 二 ) 基本概念
    pytorch深度学习实战lesson14
    互联网采集数据有哪几种常见的方法?
    【Vue2.x源码系列01】响应式原理
    New Concept English3 Lesson 1. A puma at large【精讲学习笔记】
    #力扣:2651. 计算列车到站时间@FDDLC
  • 原文地址:https://blog.csdn.net/u013075460/article/details/126839522