自定义数字键盘(kotlin)

奚高扬
2023-12-01

最近做一个数字键盘的自定义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)

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

                lateinit var keyList : MutableList<String>
                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<RecyclerView>(R.id.rv_keyboard_number)

                    tvClosePop = findViewById<TextView>(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()
    }


    }

}

 类似资料: