gpt4 book ai didi

android - 覆盖 getText 的 EditText

转载 作者:太空宇宙 更新时间:2023-11-03 13:39:47 24 4
gpt4 key购买 nike

我想覆盖 EditText 的 getText()

我收到这种字符串:"12,345,678"

我的目的是只删除逗号并返回 Editable,但是当我使用我的代码时出现错误。

public class AmountEditText extends EditText {
@Override
public Editable getText() {
Editable s = super.getText();
if(s!=null && s.length()>0) {
if (s.toString().contains(",")) {
return new SpannableStringBuilder(s.toString().replace(",", ""));
}
}
return s;
}
private TextWatcher watcher = new TextWatcher() {



@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int position = getSelectionStart();
int nbCommaBefore;
int nbCommaAfter;
String str = s.toString();
String finalStr;
String formattedStr;
nbCommaBefore = str.length() - str.replace(",", "").length();
boolean containsDot = false;
if (str.contains(".")) {
containsDot = true;
formattedStr = str.split("\\.")[0];

} else {
formattedStr = str;
}
if (!s.toString().isEmpty()) {
removeTextChangedListener(watcher);
formattedStr = formattedStr.replace(",", "");
formattedStr = formattedStr.replaceAll("(\\d)(?=(\\d{3})+$)", "$1,");
if (containsDot) {
if (str.split("\\.").length != 1) {
finalStr = formattedStr + "." + str.split("\\.")[1].replace(",", "");
} else {
finalStr = formattedStr + ".";
}
} else {
finalStr = formattedStr;
}
nbCommaAfter = finalStr.length() - finalStr.replace(",", "").length();
setText(finalStr);
if (position == str.length()){
setSelection(finalStr.length());
}
else if (position == 0)
{
setSelection(0);
}
else if (nbCommaBefore < nbCommaAfter){
setSelection(position + 1);
}

else if (nbCommaAfter < nbCommaBefore){
setSelection(position - 1);
}
else{
setSelection(position);
}
addTextChangedListener(watcher);
}
}

@Override
public void afterTextChanged(Editable s) {
}

};

public AmountEditText(Context context) {
this(context, null);
}

public AmountEditText(Context context, AttributeSet attrs) {
super(context, attrs);
addTextChangedListener(watcher);
}

public AmountEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
addTextChangedListener(watcher);
}
}

E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback E/MessageQueue-JNI: java.lang.IndexOutOfBoundsException: setSpan (0 ... 5) ends beyond length 4 at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1265) at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:684) at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:677) at android.widget.SpellChecker$SpellParser.setRangeSpan(SpellChecker.java:532) at android.widget.SpellChecker$SpellParser.parse(SpellChecker.java:515) at android.widget.SpellChecker.spellCheck(SpellChecker.java:242) at android.widget.Editor.updateSpellCheckSpans(Editor.java:679) at android.widget.Editor.sendOnTextChanged(Editor.java:1249) at android.widget.TextView.sendOnTextChanged(TextView.java:8191) at android.widget.TextView.setText(TextView.java:4483) at android.widget.TextView.setText(TextView.java:4337) at android.widget.EditText.setText(EditText.java:89) at android.widget.TextView.setText(TextView.java:4312) at org.newtonproject.newpay.widgetlib.AmountEditText$1.onTextChanged(AmountEditText.java:74)

我想明确指出错误不是来 self 的 onTextChanged因为没有 getText() override

一切正常

EDIT :用户可以输入数字,我会附加一些逗号以格式化数字。但是当我覆盖 getText() 时,我想以这种方式删除这些逗号,我不必每次都过滤 getText() 的返回

最佳答案

好的,我调试了一下,发现问题就在那一行

if (position == str.length()){
setSelection(finalStr.length());
}

lenght() 超出集合选择范围,因为它是基于 0 的

只需更改您的代码即可正常工作

if (position == str.length()){
setSelection(finalStr.length() - 1);
}

如果需要,完整代码在这里(我使用 AppCompatEditText,但它是一样的):

public class AmountEditText extends android.support.v7.widget.AppCompatEditText {
@Override
public Editable getText() {
Editable s = super.getText();
if(s!=null && s.length()>0) {
if (s.toString().contains(",")) {
return new SpannableStringBuilder(s.toString().replace(",", ""));
}
}
return s;
}
private TextWatcher watcher = new TextWatcher() {



@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int position = getSelectionStart();
int nbCommaBefore;
int nbCommaAfter;
String str = s.toString();
String finalStr;
String formattedStr;
nbCommaBefore = str.length() - str.replace(",", "").length();
boolean containsDot = false;
if (str.contains(".")) {
containsDot = true;
formattedStr = str.split("\\.")[0];

} else {
formattedStr = str;
}
if (!s.toString().isEmpty()) {
removeTextChangedListener(watcher);
formattedStr = formattedStr.replace(",", "");
formattedStr = formattedStr.replaceAll("(\\d)(?=(\\d{3})+$)", "$1,");
if (containsDot) {
if (str.split("\\.").length != 1) {
finalStr = formattedStr + "." + str.split("\\.")[1].replace(",", "");
} else {
finalStr = formattedStr + ".";
}
} else {
finalStr = formattedStr;
}
nbCommaAfter = finalStr.length() - finalStr.replace(",", "").length();
setText(finalStr);
if (position == str.length()){
setSelection(finalStr.length() - 1);
}
else if (position == 0)
{
setSelection(0);
}
else if (nbCommaBefore < nbCommaAfter){
setSelection(position + 1);
}

else if (nbCommaAfter < nbCommaBefore){
setSelection(position - 1);
}
else{
setSelection(position);
}
addTextChangedListener(watcher);
}
}

@Override
public void afterTextChanged(Editable s) {
}

};

public AmountEditText(Context context) {
this(context, null);
}

public AmountEditText(Context context, AttributeSet attrs) {
super(context, attrs);
addTextChangedListener(watcher);
}

public AmountEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
addTextChangedListener(watcher);
}
}

如果有帮助,请告诉我!

关于android - 覆盖 getText 的 EditText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55489303/

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