715a7ac7d8d0

1558004600671_video.gif

布局文件

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/parentView"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:focusableInTouchMode="true"

android:paddingStart="16dp"

android:paddingTop="20dp"

android:paddingEnd="16dp"

android:paddingBottom="20dp"

tools:ignore="MissingDefaultResource">

android:id="@+id/tvTitle"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textColor="@color/colorTextBlack"

android:textSize="16sp"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent"

tools:text="门店月租金" />

android:id="@+id/edMoney"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_marginStart="50dp"

android:background="@null"

android:gravity="end"

android:hint="¥请输入金额"

android:inputType="number"

android:textColor="@color/colorPrimary"

android:textColorHint="#B2B7BE"

android:textSize="18sp"

app:layout_constraintBaseline_toBaselineOf="@+id/tvTitle"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toEndOf="@+id/tvTitle" />

android:id="@+id/tvUnit"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginEnd="3dp"

android:text="¥"

android:textColor="@color/colorPrimary"

android:textSize="14sp"

android:visibility="gone"

app:layout_constraintBaseline_toBaselineOf="@+id/tvTitle"

app:layout_constraintEnd_toStartOf="@+id/edMoneyGone" />

android:id="@+id/edMoneyGone"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@null"

android:clickable="false"

android:focusable="false"

android:longClickable="false"

android:textColor="@color/colorPrimary"

android:textSize="18sp"

android:visibility="invisible"

app:layout_constraintBaseline_toBaselineOf="@+id/tvTitle"

app:layout_constraintEnd_toEndOf="parent" />

代码实现

/**

* Created by liuqian on 2019/3/14 19:36

* Describe:

*/

class StoreCostSetCell @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : ConstraintLayout(context, attrs, defStyleAttr) {

private val view: View = LayoutInflater.from(context).inflate(R.layout.store_cell_cost_set, this)

private val tvTitle: TextView

private val edMoney: EditText

private val tvUnit: TextView

private val edMoneyGone: EditText //不可见的那个edittext 用来控制单位的位置

init {

val a = context.obtainStyledAttributes(attrs, R.styleable.StoreCostSetCell)

val tvLeftContent = a.getString(R.styleable.StoreCostSetCell_store_LeftTitle)

val tvRightContent = a.getString(R.styleable.StoreCostSetCell_store_RightMoney)

a.recycle()

tvTitle = view.findViewById(R.id.tvTitle)

edMoney = view.findViewById(R.id.edMoney)

tvUnit = view.findViewById(R.id.tvUnit)

edMoneyGone = view.findViewById(R.id.edMoneyGone)

setListener()

tvTitle.text = tvLeftContent

edMoney.setText(tvRightContent)

}

private fun setEdHint(isShowHintConent: Boolean = true) {

if (isShowHintConent)

edMoney.hint = SpannableString("¥请输入金额").apply {

setSpan( //修改颜色

ForegroundColorSpan(Color.parseColor("#B2B7BE")),

0,

this.length,

Spanned.SPAN_EXCLUSIVE_EXCLUSIVE

)

setSpan( //单位的大小

RelativeSizeSpan(0.78f),

0,

1,

Spanned.SPAN_EXCLUSIVE_EXCLUSIVE

)

}

else edMoney.hint = ""

}

private fun setListener() {

edMoney.setOnFocusChangeListener { _, hasFocus ->

if (hasFocus) {

if (edMoney.length() == 0) {

setEdHint(false) //便显示hint

tvUnit.visibility = View.VISIBLE

}

} else {

val content = edMoney.text.toString()

if (content.isNotEmpty()) { //有内容

tvUnit.visibility = View.VISIBLE

} else {

setEdHint(true) //显示hint

tvUnit.visibility = View.GONE

}

}

}

edMoney.addTextChangedListener(object : TextWatcher {

override fun afterTextChanged(s: Editable?) {

}

override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

}

override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

//以下为加入分割逗号 千位分割

if (!edMoney.hasFocus()) return

val realString = s.toString().replace(",", "")

val stringBuild = StringBuilder() //保存变化的数据

if (realString.length > 3) { //大于 3 才会添加

//开始下标

var startAddIndex: Int = if (realString.length % 3 == 0) {

3

} else {

realString.length % 3

}

for (i in 0 until realString.length) {

if (startAddIndex == i) {

stringBuild.append(",")

startAddIndex += 3 //下一个添加逗号的下标

}

stringBuild.append(realString[i].toString())

}

if (stringBuild.toString() != s.toString()) { //别进入死循环

edMoney.setText(stringBuild.toString())

edMoney.setSelection(edMoney.length())

edMoneyGone.text = edMoney.text

}

} else {

//此时可能会存在逗号 1,23

if (edMoney.text.toString() != realString) {

edMoney.setText(realString)

edMoney.setSelection(edMoney.length())

}

edMoneyGone.setText(realString)

}

}

})

}

private fun setRightContent(string: String) {

edMoney.setText(string)

}

private fun getMoney(): String {

return edMoney.text.toString().replace(",", "")

}

}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐