2013-12-17 23 views
2

因此,我正在制作一个包含发送文本的应用程序。这将通过服务器发送,并且不会与电话(公司)本身有任何关系。检测并计算EditText中的特殊字符 - Android

我们(我的公司和我)选择放置最大数量的字符。 - 612.

所以,你可以使用612个字符空格。

我注意到特殊字符,如:é,ñ,á,è,à等...使用超过1个点。 由于我们工作的国家(西班牙)使用了很多特殊字符,我们选择了如果人类在其中一个特殊字符中,而不是使用1个点,则使用2个点。

所以基本上我想要做的是,当人打字时,计算特殊字符的数量和“-1”字符数量。

我还没有尝试太多说实话,因为我找不到与这种情况有关的任何事情。

这是我的方法。 (哦,还有的EditText +按钮等是在对话框中。)

private void smsPopUp() { 
     // TODO Auto-generated method stub 
     final Dialog smsDialog = new Dialog(this); 
     smsDialog.setContentView(R.layout.sms_dialog); 


     smsDialog.setCanceledOnTouchOutside(false); 
     Button cancelsms = (Button)smsDialog.findViewById(R.id.smsCancel); 
     EditText SmsText = (EditText) smsDialog.findViewById(R.id.etSmsText); 

     final TextView dialogCharCount = (TextView) smsDialog.findViewById(R.id.tvCharCount); 

     SmsText.addTextChangedListener(new TextWatcher(){ 
      int i = 0; 
      @Override 
      public void afterTextChanged(Editable s) { 
       // TODO Auto-generated method stub 
       i = 612 - s.length(); 
       dialogCharCount.setText(String.valueOf(i) + " Characters Remaining.."); 

      } 

      public void beforeTextChanged(CharSequence s, int start, int count, int after){} 
      public void onTextChanged(CharSequence s, int start, int before, int count){} 

     }); 



     smsDialog.setTitle("To: " + numberfield.getText()); 

     cancelsms.setOnClickListener(new OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      {   

       smsDialog.dismiss(); 
      } 
     }); 

     smsDialog.show(); 

    } 

enter image description here

(该应用程序看起来不好,但它是一个开始!)

---目前代码 - -

SmsText.addTextChangedListener(new TextWatcher(){ 
      int i = 0; 
      @Override 
      public void afterTextChanged(Editable s) { 
       // TODO Auto-generated method stub 
       String str = s.toString(); 
       int specialCounter = 0; 
       //Loop through chars 
       for (int i = 0, len = str.length(); i < len; ++i) { 
        Character c = str.charAt(i); 
        if (c == 'ñ' 
        || c == 'á' || c == 'à' || c == 'ã' || c == 'â' || c == 'ä' //a 
        || c == 'Á' || c == 'À' || c == 'Ã' || c == 'Â' || c == 'Ä' //A 
        || c == 'é' || c == 'è' || c == 'ÿ' || c == 'ê' || c == 'ë' //e + y 
        || c == 'É' || c == 'È' || c == 'Ÿ' || c == 'Ê' || c == 'Ë' //E + Y 
        || c == 'í' || c == 'ì'    || c == 'î' || c == 'ï' //i 
        || c == 'Í' || c == 'Ì'    || c == 'Î' || c == 'Ï' //I 
        || c == 'ó' || c == 'ò' || c == 'õ' || c == 'ô' || c == 'ö' //o      
        || c == 'Ó' || c == 'Ò' || c == 'Õ' || c == 'Ô' || c == 'Ö' //O 
        || c == 'ú' || c == 'ù'    || c == 'û' || c == 'ü' //u 
        || c == 'Ú' || c == 'Ù'    || c == 'Û' || c == 'ü' //U 
        ) { 
         specialCounter++; 
        } 
       } 

       i = 612 - s.length() - specialCounter; 
       dialogCharCount.setText(String.valueOf(i) + " Characters Remaining.."); 

      } 

      public void beforeTextChanged(CharSequence s, int start, int count, int after){} 
      public void onTextChanged(CharSequence s, int start, int before, int count){} 

     }); 

这是垃圾。我知道。直到我找到更智能,更快捷的方式,它才能完成工作。

+0

http://stackoverflow.com/questions/16160989/how-to-detect-special-characters-in-an-edit-text-and-display- a-toast-in-response看到这个链接 – Meenal

+0

@MeenalSharma我有,我试图修改该代码,并适合我的。仍在尝试,谢谢! – Paramone

回答

2

通过插入的文本,只要运行和比较字符

Set<Character> specialChars = new HashSet<Character>(); 
specialChars.add('é'); 
// ... 
specialChars.add('ñ'); 

String str = s.toString(); 
int specialCounter = 0; 
for (int i = 0, len = str.length(); i < len; ++i) { 
    Character c = str.charAt(i); 
    if (specialChars.contains(c)) { 
     specialCounter++; 
    } 
} 

int totalRemaining = 612 - s.length() - specialCounter; 
+0

这正是我的想法,只需增加一个计数器,并最终减少计数器的数量。 但问题是,我将不得不为每一个“特殊字符”制作数百个“If”语句。我正在寻找特殊角色的“包裹”。如果一个角色在这个“特殊字符包”里面.. SpecialCounter ++。 – Paramone

+0

看到我编辑的答案。只需定义一些字符集并检查字符是否在该集合中。 – holtaf

+0

啊,我应该这样做..我得到了:if(c =='ñ'|| c =='á'|| c =='à'... etc)..“...等等“我的意思是,60多个”或“的.. – Paramone

2

我建议去正则表达式做这样的任务,它会给你更好的效率以及它更容易使用,从长远来看。

,你可以尝试这样的事情

静态INT countAlphabets,countSpecialCharacter;

String mesage =“这是识别$字和特殊$%^字符的演示文本!”@“;

String pattern4 =“[aA-zZ]”; // REGULAR EXPRESSION用于计数字符 String pattern5 =“!| @ | $ |%| ^”; //计数特殊字符

Pattern pattern11 = Pattern.compile(pattern4); 
Matcher matcher = pattern11.matcher(mesage); 
while(matcher.find()) 
{ 
    System.out.println("no of words are "+countAlphabets+++" and still" 
      + " counting"); 

} 
Pattern pattern22= Pattern.compile(pattern5); 
Matcher matcher1= pattern22.matcher(mesage); 
while(matcher1.find()) 
{ 
    System.out.println("no of words are "++countSpecialCharacter+++" and still" 
      + " counting"); 

} 

leeme知道它有用的正则表达式。

+0

Hey Khay,感谢回答。这很有用,我不得不承认。但我不知道我是否会使用它,除非我改变模式。因为不是所有的特殊字符都应该作为“两个”字符点。只有几个,就像手机上的短信一样。谢谢你的帮助! – Paramone

+1

是的,所以你可以让那些在上面的pattern5中被计数为2的那些,并且你可以保留在pattern4 <上面的代码中> – Khay

0

,你也可以检查,如:

if(!Character.isLetter(str.charAt(i))) { 
    //its a special character 
    //increase count 
} 
else { 
    //its a letter 
}