gpt4 book ai didi

java - 如何删除屏蔽的edittext内容

转载 作者:行者123 更新时间:2023-12-01 16:56:29 24 4
gpt4 key购买 nike

例如:“2020/55”我有一个编辑文本形状,在4个数字后面放一个,在后面写一个数字,效果很好。我的问题从删除时开始。删除/后面的部分时,会显示“/////”/前面部分的数字为 和 。我添加了你的图片,我怎么删除它。 enter image description here

和掩码类

public class CaseInputMask implements TextWatcher {
int uzunluk = 0;
EditText girilenMetin;

public CaseInputMask(EditText girilenMetin) {
this.girilenMetin = girilenMetin;
this.girilenMetin.addTextChangedListener(this);
}

@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
String metin = girilenMetin.getText().toString();
uzunluk = metin.length();
}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
try {
String metin = charSequence.toString();
String girilenDeger = girilenMetin.getText().toString();
if (girilenDeger.length() == 4) {
metin += '/';
girilenMetin.setText(metin);
girilenMetin.setSelection(metin.length());
}
} catch (Exception e) {

}
}

@Override
public void afterTextChanged(Editable editable) {

}
}

最佳答案

您应该像这样检查删除:

public class CaseInputMask implements TextWatcher {
private boolean running = false;
private boolean deleting = false;
private final String inputMask = "####/##";

public CaseInputMask() {

}

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

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

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

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

running = false;
}
}

girilenMetin.addTextChangedListener(new CaseInputMask())

关于java - 如何删除屏蔽的edittext内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61581961/

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