6 java android android-edittext kotlin
我有一个这样的字符串 08:12:01,868->hour:minute:second,millisecond
现在我需要编辑这个,我必须提供一个选项来编辑前两个字符跳过一个:然后允许再次编辑下两个字符跳过:等等。
目前我正在实现这一点,背后有很多逻辑。我有四个编辑文本,我正在拆分字符串,然后在 4 个编辑文本中加载值并检索它。加入所有编辑过的子字符串后,我将值保存在数据库中。
有没有其他更好的方法来实现这一点。
我的意思是我只会在一个编辑文本中加载字符串,然后我应该能够锁定特定字符lockFromEditChar(":",","),如果它们清除所有字符,则可以使用默认值零编辑剩余值。
是否可以 ?或任何其他最佳选择?
如果您将光标挡在最后一个位置并且禁用了长点击(复制粘贴):
请注意,此示例仅适用于字符串,不适用于跨度
创建一个 TextWatcher 并自动添加 : 和 , 当您检测到添加了新字符并且新大小为 2,4(对于 ':'),6(对于 ',')。
还要检测当你删除一个字符并且当前大小是2,4,6时,你必须删除';'之前的字符 或“,”也是如此。
class Watcher(private val textView:TextView) : TextWatcher {
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
val text = s?.toString()?:""
val isDelete = textView.length > text.size
if(isDelete) {
//you can change this to an if to check for 2,4,6 and use take(size-1)
newString = when(text.size) {
2->text.take(1)
4->text.take(3)
6->text.take(5)
}
if(newString!=null) {
textView.text = newString
}
} else {
newString = when(text.size) {
2,4 -> text + ":"
6 -> text + ","
}
if(newString!=null) {
textView.text = newString
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
175 次 |
| 最近记录: |