2013-12-17 26 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 
}