【发布时间】:2019-12-18 07:13:37
【问题描述】:
我想要一个编辑文本,其值为 0.00,并且光标必须在左侧。当用户按下 1 时,它应该是 1.00,当用户按下 2 时,它应该是 12.00,当用户按下“。”时,它应该是 12.00。光标应移动到小数点的另一侧。当用户按 3 移动到小数点的另一侧后,它应该是 12.30,按 4 应该是 12.34。同样,在背压上它应该是 12.30 -> 12.00 -> 1.00 -> 0.00.sample
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class AmountTextWatcher implements TextWatcher {
private EditText editText;
private int num_npt;
private String previous;
private boolean isBackSpacePressed;
public AmountTextWatcher(EditText editText, int num_npt) {
this.editText = editText;
this.num_npt = num_npt;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
/*if (StorageUtils.INTERNAL_TRANSFER_AMOUNT != 0.00)
previous = String.format(Locale.getDefault(), "%.2f", StorageUtils.INTERNAL_TRANSFER_AMOUNT);
else*/
if (after < count) {
isBackSpacePressed = true;
} else {
isBackSpacePressed = false;
}
previous = editText.getText().toString();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
editText.removeTextChangedListener(this);
if (isBackSpacePressed) {
String old_val = previous;
int point = old_val.indexOf(".");
if (point == -1) point = old_val.length();
int selectionStart = editText.getSelectionStart() + 1;
int selectionEnd = editText.getSelectionEnd();
String new_val = "";
if (selectionStart < selectionEnd) {
if (selectionEnd <= point) {
new_val = old_val.substring(0, selectionStart) + old_val.substring(selectionEnd);
} else if (selectionStart > point) {
new_val = old_val.substring(0, selectionStart) + "0" + old_val.substring(selectionEnd);
if (selectionEnd - selectionStart == num_npt) new_val += "0";
} else if (selectionStart <= point && selectionEnd > point) {
new_val = old_val.substring(0, selectionStart) + ".";
for (int z = 0; z < selectionEnd - point - 1; z++) new_val += "0";
new_val += old_val.substring(selectionEnd);
}
if (new_val.indexOf(".") == 0) new_val = "0" + new_val;
} else if (selectionStart == 1 && point == 1) {
new_val = "0" + old_val.substring(1);
selectionStart = 0;
} else if (selectionStart > 0 && selectionStart <= point) {
new_val = old_val.substring(0, selectionStart - 1) + old_val.substring(selectionStart);
selectionStart--;
} else if (selectionStart == point + 1) {
new_val = old_val;
selectionStart = point;
} else if (selectionStart > point + 1) {
new_val = old_val.substring(0, selectionStart - 1) + old_val.substring(selectionStart) + "0";
selectionStart--;
} else {
new_val = old_val;
}
if (new_val.equals("")) new_val = "0";
editText.setText(new_val);
editText.setSelection(selectionStart);
editText.addTextChangedListener(this);
} else {
int point = previous.indexOf(".");
if (point == -1) point = previous.length();
int selectionStart = editText.getSelectionStart() - 1;
int selectionEnd = editText.getSelectionEnd() - 1;
String c = s.subSequence(start, start + count).toString();
if (c.equals("-") || c.equals(" ")) {
editText.setText(previous);
editText.setSelection(selectionStart);
editText.addTextChangedListener(this);
return;
}
String new_val = "";
if (selectionStart > -1 && selectionStart < editText.getText().length() - 1) {
if (c != null && c.length() > 0) {
if (c.equals(",")) c = ".";
if (Character.isDigit(c.charAt(0)) || c.equals(".")) {
if (selectionStart < selectionEnd) {
if (selectionEnd <= point) {
new_val = previous.substring(0, selectionStart) + c + previous.substring(selectionEnd);
} else if (selectionStart > point) {
new_val = previous.substring(0, selectionStart) + c + previous.substring(selectionEnd);
for (int x = 0; x < selectionEnd - selectionStart - 1; x++)
new_val += "0";
} else if (selectionStart <= point && selectionEnd > point && !c.equals(".")) {
new_val = previous.substring(0, selectionStart) + c + ".";
for (int y = 0; y < selectionEnd - point - 1; y++)
new_val += "0";
new_val += previous.substring(selectionEnd);
}
} else if ((num_npt == 0 && previous.equals("0")) || (previous.substring(0, 2).equals("0.") && selectionStart == 0)) {
if (c.equals(".")) {
new_val = previous;
editText.setSelection(0);
} else
new_val = c + previous.substring(1);
} else if (selectionStart > point && selectionStart <= point + num_npt) {
if (c.equals(".")) {
if (editText.getSelectionStart() > previous.indexOf("."))
new_val = previous.substring(0, selectionStart) + previous.substring(selectionStart);
else
new_val = previous.substring(0, selectionStart - 1) + c + previous.substring(selectionStart);
editText.setSelection(selectionStart - 1);
} else
new_val = previous.substring(0, selectionStart) + c + previous.substring(selectionStart + 1);
}
// this is the code where our edittext lags :(
else {
if (c.equals("."))
if (editText.getSelectionStart() - 1 == 0) {
if (c.equals("."))
new_val = previous.substring(0, 1) + previous.substring(selectionEnd + 1);
else
new_val = previous.substring(0, 1) + c + previous.substring(selectionEnd + 2);
} else {
if (selectionStart > 0 && selectionStart < previous.indexOf("."))
new_val = previous.substring(0, selectionStart + 1) + previous.substring(selectionEnd + 1);
else
new_val = previous.substring(0, selectionStart) + c + previous.substring(selectionEnd + 1);
}
else
new_val = previous.substring(0, selectionStart) + c + previous.substring(selectionEnd);
if (new_val.substring(new_val.length() - 1).equals("."))
new_val = new_val.substring(0, new_val.length() - 1);
}
// *********//
if (new_val.substring(0, 1).equals("0") && !new_val.substring(0, 2).equals("0.")) {
new_val = new_val.substring(1);
selectionStart--;
}
Pattern pattern = Pattern.compile("^[0-9]+(\\.[0-9]{0," + num_npt + "})?$");
Matcher matcher = pattern.matcher(new_val);
boolean doNothing = false;
if (matcher.matches()) {
editText.setText(new_val);
if (!c.equals("."))
selectionStart++;
else if (c.equals(".") && selectionStart == previous.indexOf("."))
selectionStart++;
editText.setSelection(selectionStart);
} else if (c.equals(".")) {
selectionStart++;
editText.setSelection(selectionStart);
}
editText.addTextChangedListener(this);
}
} else {
editText.setText(previous);
editText.setSelection(selectionStart);
editText.addTextChangedListener(this);
}
} else {
int length = editText.getText().length();
if (selectionStart < 0)
selectionStart = 0;
else if (selectionStart > length || selectionEnd < length)
selectionStart = length - 1;
editText.setText(previous);
editText.setSelection(selectionStart);
editText.addTextChangedListener(this);
}
}
}
@Override
public void afterTextChanged(Editable s) {
}
}
【问题讨论】:
-
这段代码可以随心所欲地工作。你面临的困难是什么?
-
这不是好的解决方案。它在写作时滞后。错过很多数字,用户体验很差
-
@Md 你试过这个代码吗?
-
有没有人找到一些优化的答案?
标签: java android decimal currency-formatting