如果 if else 语句成功,我正在尝试更改 EditText 边框。我有 4 个类似的 EditText。然而其中第一个,第二个表现得有点奇怪,验证成功后它的大小会扩大。所有尺寸、布局一切都相同(我只是复制和粘贴),但为什么第二个 EditText 表现得很奇怪?
问题是如何确保EditText的大小与其他EditText的大小一致且相同。
代码如下
button.setOnClickListener {
val brnText = brn_TextBox.text.toString().trim()
val tinText = tin_TextBox.text.toString().trim()
if (isValidBrn(brnText)) {
brn_TextBox.setBackgroundResource(R.drawable.edittext_green_border)
brn_TextBox.isEnabled = false
showDrawable(brn_TextBox)
} else {
brn_TextBox.setBackgroundResource(R.drawable.edittext_red_border)
brn_TextBox.setOnClickListener{
brn_TextBox.setBackgroundResource(R.drawable.edittext_border)
}
brn_TextBox.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
brn_TextBox.setBackgroundResource(R.drawable.edittext_border)
}
override fun afterTextChanged(s: Editable?) {}
})
}
if (isValidTin(tinText)) {
tin_TextBox.setBackgroundResource(R.drawable.edittext_green_border)
tin_TextBox.isEnabled = false
showDrawable(tin_TextBox)
} else {
tin_TextBox.setBackgroundResource(R.drawable.edittext_red_border)
tin_TextBox.setOnClickListener{
tin_TextBox.setBackgroundResource(R.drawable.edittext_border)
}
tin_TextBox.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
tin_TextBox.setBackgroundResource(R.drawable.edittext_border)
}
override fun afterTextChanged(s: Editable?) {}
})
}
if(isValidTin(tinText) && isValidBrn(brnText)){
button.isEnabled = false
button.visibility = View.GONE
}
}
//底部基本上是相同的东西,只是名称不同
button2.setOnClickListener {
val msicText = msic_TextBox.text.toString().trim()
val sstText = sst_TextBox.text.toString().trim()
if (isValidMsic(msicText)) {
msic_TextBox.setBackgroundResource(R.drawable.edittext_green_border)
msic_TextBox.isEnabled = false
showDrawable(msic_TextBox)
} else {
msic_TextBox.setBackgroundResource(R.drawable.edittext_red_border)
msic_TextBox.setOnClickListener{
msic_TextBox.setBackgroundResource(R.drawable.edittext_border)
}
msic_TextBox.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
tin_TextBox.setBackgroundResource(R.drawable.edittext_border)
}
override fun afterTextChanged(s: Editable?) {}
})
}
if (isValidSst(sstText)) {
sst_TextBox.setBackgroundResource(R.drawable.edittext_green_border)
sst_TextBox.isEnabled = false
showDrawable(sst_TextBox)
} else {
sst_TextBox.setBackgroundResource(R.drawable.edittext_red_border)
sst_TextBox.setOnClickListener{
sst_TextBox.setBackgroundResource(R.drawable.edittext_border)
}
sst_TextBox.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
tin_TextBox.setBackgroundResource(R.drawable.edittext_border)
}
override fun afterTextChanged(s: Editable?) {}
})
}
if(isValidMsic(msicText) && isValidSst(sstText)){
button2.isEnabled = false
button2.visibility = View.GONE
}
}
成功后显示对勾图标
private fun showDrawable(editText: EditText) {
val drawable = ContextCompat.getDrawable(this, R.drawable.tick_icon_svg)
drawable?.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight)
editText.setCompoundDrawables(drawable, null, null, null)
}
xml 文件
brn_EditText(编辑文本1)
<EditText
android:id="@+id/brn_TextBox"
android:layout_width="match_parent"
android:textColor="@color/edittext_text_color"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/_32sdp"
android:layout_marginEnd="@dimen/_32sdp"
android:layout_marginBottom="@dimen/_31sdp"
android:background="@drawable/edittext_border"
android:drawableStart="@drawable/tick_icon_svg"
android:drawableTint="@color/colorGreen"
android:ems="10"
android:gravity="center"
android:hint="@string/tin_eg"
android:importantForAutofill="no"
android:inputType="number"
android:maxLength="12"
android:padding="@dimen/_14sdp"
android:textCursorDrawable="@drawable/cursor_textinput_edit_text" />
tin_EditText(EditText2)
<EditText
android:id="@+id/tin_TextBox"
android:textColor="@color/edittext_text_color"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/_32sdp"
android:layout_marginEnd="@dimen/_32sdp"
android:layout_marginBottom="@dimen/_31sdp"
android:background="@drawable/edittext_border"
android:drawableStart="@drawable/tick_icon_svg"
android:drawableTint="@color/colorGreen"
android:ems="10"
android:gravity="center"
android:hint="@string/tin_eg"
android:importantForAutofill="no"
android:maxLength="15"
android:maxLines="1"
android:padding="@dimen/_14sdp"
android:textCursorDrawable="@drawable/cursor_textinput_edit_text" />
msic_EditText(EditText3)
<EditText
android:id="@+id/msic_TextBox"
android:textColor="@color/edittext_text_color"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/_32sdp"
android:layout_marginEnd="@dimen/_32sdp"
android:layout_marginBottom="@dimen/_31sdp"
android:background="@drawable/edittext_border"
android:drawableStart="@drawable/tick_icon_svg"
android:drawableTint="@color/colorGreen"
android:ems="10"
android:maxLength="5"
android:gravity="center"
android:hint="@string/msic_eg"
android:inputType="numberSigned"
android:padding="@dimen/_14sdp"
android:textCursorDrawable="@drawable/cursor_textinput_edit_text" />
sst_editText(EditText4)
<EditText
android:id="@+id/sst_TextBox"
android:textColor="@color/edittext_text_color"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/_32sdp"
android:layout_marginEnd="@dimen/_32sdp"
android:layout_marginBottom="@dimen/_31sdp"
android:background="@drawable/edittext_border"
android:drawableStart="@drawable/tick_icon_svg"
android:drawableTint="@color/colorGreen"
android:ems="10"
android:maxLength="15"
android:gravity="center"
android:hint="@string/sst_eg"
android:importantForAutofill="no"
android:inputType="number"
android:padding="@dimen/_14sdp"
android:textCursorDrawable="@drawable/cursor_textinput_edit_text" />
试试这个代码:
private fun handleValidation(editText: EditText, isValid: Boolean) {
if (isValid) {
editText.setBackgroundResource(R.drawable.edittext_green_border)
editText.isEnabled = false
showDrawable(editText)
} else {
editText.setBackgroundResource(R.drawable.edittext_red_border)
editText.setOnClickListener {
editText.setBackgroundResource(R.drawable.edittext_border)
}
editText.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
editText.setBackgroundResource(R.drawable.edittext_border)
}
override fun afterTextChanged(s: Editable?) {}
})
}
}
button.setOnClickListener {
val brnText = brn_TextBox.text.toString().trim()
val tinText = tin_TextBox.text.toString().trim()
handleValidation(brn_TextBox, isValidBrn(brnText))
handleValidation(tin_TextBox, isValidTin(tinText))
if (isValidBrn(brnText) && isValidTin(tinText)) {
button.isEnabled = false
button.visibility = View.GONE
}
}