gpt4 book ai didi

java - java中的十进制输入edittext

转载 作者:行者123 更新时间:2023-12-01 19:18:38 24 4
gpt4 key购买 nike

我想要一个具有诸如 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) {

}
}

最佳答案

尝试在Java中使用DecimalFormat类。它可能会以更好的方式帮助您。

String pattern = "##.##";
DecimalFormat decimalFormat = new DecimalFormat(pattern);
String output = decimalFormat.format( YOUR NUMBER HERE );

关于java - java中的十进制输入edittext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59387099/

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