gpt4 book ai didi

java - 如何避免在编辑文本中编辑特定字符

转载 作者:行者123 更新时间:2023-12-02 11:54:32 25 4
gpt4 key购买 nike

我有一个这样的字符串 08:12:01,868 -> hour:minute:second,millisecond现在我需要编辑这个,我必须给 : 编辑前两个字符跳过一个选项然后允许再次编辑下两个字符跳过:等等。
目前我正在实现这一点,背后有很多逻辑。我有四个编辑文本,我正在拆分字符串,然后在 4 个编辑文本中加载值并检索它。加入所有已编辑的子字符串后,我将值保存在 DB 中。
有没有其他更好的方法来实现这一点。
我的意思是我只会在一个编辑文本中加载字符串,然后我应该能够锁定特定字符,如 lockFromEditChar(":",",")如果清除所有字符,则可以使用默认值零编辑剩余值。
可能吗 ?或任何其他最佳选择?

最佳答案

该技术称为掩蔽。创建此类MaskWatcher像这样

import android.text.Editable;
import android.text.TextWatcher;

public class MaskWatcher implements TextWatcher {
private boolean isRunning = false;
private boolean isDeleting = false;
private final String mask;

public MaskWatcher(String mask) {
this.mask = mask;
}

@Override
public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
isDeleting = count > after;
}

@Override
public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
}

@Override
public void afterTextChanged(Editable editable) {
if (isRunning || isDeleting) {
return;
}
isRunning = true;

int editableLength = editable.length();
if (editableLength < mask.length()) {
if (mask.charAt(editableLength) != '#') {
editable.append(mask.charAt(editableLength));
} else if (mask.charAt(editableLength-1) != '#') {
editable.insert(editableLength-1, mask, editableLength-1, editableLength);
}
}

isRunning = false;
}
}
而不是像这样使用你的edittext editText.addTextChangedListener(MaskWatcher("##:##:##,###"));

关于java - 如何避免在编辑文本中编辑特定字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64573799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com