在Android的EditText中,有没有一种方法可以强制使每个字符都变为小写?

huangapple go评论94阅读模式
英文:

In Android EditText, is there a way to force every character to be lowercase?

问题

我在这里找到了很多关于使EditText显示大写字符的解决方案,但是,有没有一种方法来实现相反的效果,并将其限制为仅小写字符?

使用 android:digits="abcdefghijklmnpqrstuvwxyz" 虽然“起作用”,但它会简单地忽略不在列表中的字符,而没有任何反馈。我希望即使用户输入大写字符,它也会被转换为小写字符。
它可以忽略数字,没问题。

英文:

I found a lot of solutions here to make EditText display characters in caps, however, is there a way to do the opposite and limit it to lowercase characters only?

Using android:digits="abcdefghijklmnpqrstuvwxyz" "works" but it simply ignores characters not in the list with no feedback. I want it so that even when the user inputs an uppercase character, it is converted to a lowercase character.
<br>It can ignore numbers, that is fine.

答案1

得分: 1

以下是翻译好的内容:

Max链接了一个Java解决方案,但这里是一个Kotlin版本的:

edit_text.filters += object : AllCaps() {
    override fun filter(
        source: CharSequence,
        start: Int,
        end: Int,
        dest: Spanned,
        dstart: Int,
        dend: Int
    ): CharSequence {
        return source.toString().toLowerCase()
    }
}
英文:

Max linked to a Java solution but here is a kotlin one:


edit_text.filters += object : AllCaps() {
            override fun filter(
                source: CharSequence,
                start: Int,
                end: Int,
                dest: Spanned,
                dstart: Int,
                dend: Int
            ): CharSequence {
                return source.toString().toLowerCase()
            }
        }

huangapple
  • 本文由 发表于 2020年8月22日 01:24:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63527416.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定