【问题标题】:How to format text by whitespaces?如何通过空格格式化文本?
【发布时间】:2015-04-08 09:28:42
【问题描述】:

在 Android 应用程序中,我有一个字段,用户应在其中输入一些 14 位数字,例如 12345678901234。 我希望这个号码看起来像 12 3456 7890 1234。 我试图通过代码来做到这一点:

if((s.length() == 2 || s.length() == 7 || s.length() == 12)){
s.insert(s.length(), " ");
}

但是当用户开始在文本中间输入时,我的代码会出错。

我尝试使用 DecimalFormat 类:

DecimalFormat decimalFormat = new DecimalFormat("##,####.#### ####");
String formattedText = decimalFormat.format(Double.parseDouble(etContent.getText().toString()));

但我收到 IllegalArgumentException。 任何想法如何做到这一点? P.S 主要问题是我应该“立即”格式化文本,例如:
1
12
12 3
12 34
12 345
12 3456
12 3456 7
12 3456 78
12 3456 789
12 3456 7890
12 3456 7890 1
12 3456 7890 12
12 3456 7890 123
12 3456 7890 1234

【问题讨论】:

  • 关于哪个参数的 IllegalArgumentException?
  • new DecimalFormat("##,####.#### ####")
  • 那一行不会给..可能是第二行可以给\
  • 你应该考虑做屏蔽输入stackoverflow.com/questions/2912375/…
  • decimalFormat.format(Double.parseDouble("12345678901234")) 应该可以工作。它会给你逗号分隔的值,你可以用空格替换。

标签: java android string-formatting number-formatting decimalformat


【解决方案1】:

@Karthika PB 我认为它会删除第三个字符,即 12 4567,就像它会出现一样。 试试这个

    String seq = editText.getText().toString().trim();

    String newstring = "";
    for (int i = 0; i < seq.length(); i++) {

        if (i == 2 || i == 6 || i == 10) {
            newstring = newstring + " " + seq.charAt(i);

        } else
            newstring = newstring + seq.charAt(i);
    }

【讨论】:

    【解决方案2】:
    try this remove white spaces and insert space
    
     String seq=etContent.getText().toString().trim();//to remove white spaces
    
    
             String newstring="";
        for(int i=0;i<seq.length();i++){
    
            if(i==2 || i==7 || i==14){
                newstring=newstring+" ";
    
            }
            else
            newstring=newstring+seq.charAt(i);
        }
    

    使用具有格式化文本的新字符串

    【讨论】:

      【解决方案3】:

      只需使用子串方法:

      String formattedText = s.substring(0,2) + " " + s.substring(2,6)+
               " " + s.substring(6,10) + " " + s.substring(10,14);
      

      【讨论】:

        【解决方案4】:

        在 TextWatcher.afterTextChanged(Editable) 中试试这些代码

                @Override
                public void afterTextChanged(Editable s) {
                    if (changeText) {
                        Log.d("afterTextChanged", "afterTextChanged");
                        String txt = s.toString();
                        txt = txt.replaceAll("\\s+", "");
                        changeText = false;
                        s.clear();
                        int index = 0;
                        for (char c:txt.toCharArray()) {
                            if (index == 2
                                    || index == 7
                                    || index == 12) {
        
                                s.append(' ');
                                index++;
                            }
                            s.append(c);
                            index++;
                        }
                        changeText = true;
                    }
                }
        

        changeText 是一个防止无限循环的布尔标记。

        【讨论】:

        • 并且“changeText”的初始值应该是“true”。
        猜你喜欢
        • 1970-01-01
        • 2016-11-19
        • 2012-10-24
        • 1970-01-01
        • 2022-08-18
        • 1970-01-01
        • 1970-01-01
        • 2013-09-22
        • 1970-01-01
        相关资源
        最近更新 更多