我只想以编程方式将提示更改为大写字母,例如EditText
如下图所示。
我可以通过以下代码轻松完成此操作
var hintText:String = tip.hint.toString()
editText.onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
if (hasFocus) {
textInputLayout.hint = hintText.toUpperCase()
} else {
textInputLayout.hint = hintText
}
}
这里,我将提示存储在一个简单的字符串变量中,并将提示EditText
设置为大写字母时将提示设置为大写字母
但是我无法使用自定义EditText
这是我的CustomEditText
课:
class CustomEditText : TextInputEditText {
var isFilled: Boolean = false
lateinit var contextt: Context
constructor(context: Context) : super(context) {
contextt = context
init()
}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
contextt = context
init()
}
constructor(context: Context, attrs: AttributeSet, defStyle: Int)
: super(context, attrs, defStyle) {
contextt = context
init()
}
private fun init() {
}
override fun onTextChanged(text: CharSequence?, start: Int, lengthBefore: Int, lengthAfter: Int) {
isFilled = text.toString().isNotEmpty()
var context = context
while (context is ContextWrapper) {
if (context is Activity) {
(context as BaseAct).etFillEvent()
}
context = context.baseContext
}
}
override fun onFocusChanged(focused: Boolean, direction: Int, previouslyFocusedRect: Rect?) {
super.onFocusChanged(focused, direction, previouslyFocusedRect)
val resColor: Int = if (focused)
R.color.colorPrimary
else
R.color.grey
this.background.setColorFilter(ContextCompat.getColor(context, resColor), PorterDuff.Mode.SRC_IN)
}
fun setError() {
this.background.setColorFilter(
ContextCompat.getColor(context, R.color.red),
PorterDuff.Mode.SRC_IN
)
this.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_error, 0)
}
fun getString(): String {
return this.text.toString().trim()
}
}
如果有人可以帮助我,那就太好了