因此,我正在製作一個包含發送文本的應用程序。這將通過服務器發送,並且不會與電話(公司)本身有任何關係。檢測並計算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();
}
(該應用程序看起來不好,但它是一個開始!)
---目前代碼 - -
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){}
});
這是垃圾。我知道。直到我找到更智能,更快捷的方式,它才能完成工作。
http://stackoverflow.com/questions/16160989/how-to-detect-special-characters-in-an-edit-text-and-display- a-toast-in-response看到這個鏈接 – Meenal
@MeenalSharma我有,我試圖修改該代碼,並適合我的。仍在嘗試,謝謝! – Paramone