Android 如何在EditText中用逗号分隔数字
我有一个EditText,它的输入类型是number。当用户输入时,我想用逗号分隔数字。这里有一个小例子: 123将代表123 1234将表示为1234 12345将被表示为12345 ……等等 我尝试使用TextWatcher添加逗号,如下所示:Android 如何在EditText中用逗号分隔数字,android,android-edittext,number-formatting,Android,Android Edittext,Number Formatting,我有一个EditText,它的输入类型是number。当用户输入时,我想用逗号分隔数字。这里有一个小例子: 123将代表123 1234将表示为1234 12345将被表示为12345 ……等等 我尝试使用TextWatcher添加逗号,如下所示: EditText edittext = findViewById(R.id.cashGiven); edittext.addTextChangedListener(new TextWatcher(){ @Overr
EditText edittext = findViewById(R.id.cashGiven);
edittext.addTextChangedListener(new TextWatcher(){
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
editText.setText(separateWithComma(editText.getText().toString().trim()));
}
});
在这里粘贴separateWithComma()
方法会使这个问题变得格外冗长,但它确实有效:我在Eclipse上测试了它。我认为addTextChangedListener不能以这种方式工作,因为当我这样做时,我的应用程序会冻结(然后很晚就会崩溃)
有没有更好的方法来实现这一点?感谢您的积极响应。尝试使用
String.format
而不是现在的格式。替换此项:
editText.setText(separateWithComma(editText.getText().toString().trim()));
使用此选项:
editText.setText(String.format("%,d", your number));
另一件事-你的应用程序可能会崩溃,因为每次你在posterextchanged
内调用setText()
,就会调用另一个posterextchanged
,基本上会创建一个无限循环。如果这是您的问题,您可以找到一个好的解决方案。尝试以下代码:
et.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
et.removeTextChangedListener(this);
try {
String givenstring = s.toString();
Long longval;
if (givenstring.contains(",")) {
givenstring = givenstring.replaceAll(",", "");
}
longval = Long.parseLong(givenstring);
DecimalFormat formatter = new DecimalFormat("#,###,###");
String formattedString = formatter.format(longval);
et.setText(formattedString);
et.setSelection(et.getText().length());
// to place the cursor at the end of text
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
et.addTextChangedListener(this);
}
});
我得到了两个很好的答案,但我只想补充一点:
editText.setText(String.format("%,d", your number));
我面临此问题的原因是,在我的TextWatcher的PostTextChanged()
函数中递归调用了editText.setText(…)
。正如前面的回答者正确地指出的那样,解决方案是通过以下方式暂时停止posterextchanged()
方法的激发:
private boolean edit = true;
private int cursorPosition;
...
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
cursorPosition = editText.getText().toString().length() - editText.getSelectionStart();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count){}
@Override
public void afterTextChanged(Editable editable){
if(edit){
edit = false;
editText.setText(String.format("%,d", Integer.parseInt(editText.getText().toString().trim())));
edit = true;
}
//setting the cursor back to where it was
editText.setSelection(editText.getText().toString().length() - cursorPosition);
}
这样,我们小心翼翼地迂回前行。但这带来了一个新问题:
使用此策略,我们假设EditText中的字符串可以轻松地转换为int
。当我们输入更多的数字时,应用程序肯定会因为逗号而出现NumberFormatException崩溃。因此,找到解决这个问题的方法是很重要的。我写了一个方法来帮助我:
public String getCommalessNumber(String commaNumber){
String newNumber = "";
String[] split = commaNumber.trim().split(",");
for(int i = 0; i < split.length; i++){
newNumber += split[i];
}
return newNumber;
}
我希望这对其他人有所帮助。对于那些可能想使用这种方法的人,将getCommalesNumber(String commaNumber)方法的返回类型更改为String而不是int。同样的结果,应用程序开始疯狂地冻结。我认为我不应该使用addTextChangedListener。我认为这就是问题所在。@TaslimOseni为了防止应用程序冻结,您应该在执行
setText(“”
)时禁用textChangedListener,然后再次启用它。在PostTextChanged中设置文本的问题在于它会创建一个无限循环,请检查此答案