直播带货平台源码Android实现自定义圆形、方形输入框
直播带货平台源码Android实现自定义圆形、方形输入框的相关代码
实现思路分析:
1、 使用一个LinearLayout用来填充每一个小方格,通过动态添加,实现出需要数量的输入框
2、 在LinearLayout上覆盖一层大小和LinearLayout大小完全一致的EditText,用来接口输入信息,设置EditText输入背景和文字为透明,并设置不展示光标,
3、 监听EditText的内容变化,和LinearLayout的内容绑定,实现每次输入都由LinearLayout的子布局展示出来
布局文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="wrap_content" android:layout_height="wrap_content"> <LinearLayout android:orientation="horizontal" android:id="@+id/rvContentList" android:gravity="center" android:showDividers="middle" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <EditText android:id="@+id/inputReal" android:inputType="number" android:background="@android:color/transparent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/transparent"/> </RelativeLayout>
在代码中动态创建LinearLayout子布局填充,并绑定监听
private fun initContainer() { //动态设置EditText的大小 inputReal = findViewById(R.id.inputReal) rvContentList = findViewById(R.id.rvContentList) inputReal.width = (dividerDrawable?.minimumWidth ?: 0 * (verifyCodeLen - 1)) + inputBoxSize * verifyCodeLen inputReal.height = inputBoxSize inputReal.setTextSize(TypedValue.COMPLEX_UNIT_PX, inputTextSize * 1.0F) //禁用光标 inputReal.isCursorVisible = false inputReal.filters = arrayOf(InputFilter.LengthFilter(verifyCodeLen)) inputTextView.clear() //动态添加LinearLayout之间的分割线 dividerDrawable?.let { it.setBounds(0, 0, it.minimumWidth, it.minimumHeight) rvContentList.dividerDrawable = it } for (i in 0 until verifyCodeLen) { val textView = TextView(context) textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, inputTextSize * 1.0F) textView.width = inputBoxSize textView.height = inputBoxSize textView.gravity = Gravity.CENTER textView.isFocusable = false textView.textColor = inputTextColor textView.backgroundResource = itemSelector inputTextView.add(textView) } inputTextView.forEach { rvContentList.addView(it) } } inputReal.addTextChangedListener(object : TextWatcher { override fun afterTextChanged(p0: Editable?) { setVerifyCodeInputValue(p0.toString()) if (p0.toString().length == verifyCodeLen) { onCompleteListener?.onComplete(p0.toString()) } } override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) { } override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) { } }) private fun setVerifyCodeInputValue(inputText: String) { inputTextView.forEach { it.text = "" it.isSelected = false } inputTextView.forEachIndexed { index, textView -> if (inputText.length > index) { textView.isSelected = true textView.text = inputText[index].toString() } } }
以上就是直播带货平台源码Android实现自定义圆形、方形输入框的相关代码, 更多内容欢迎关注之后的文章