因此,我正在制作一个包含发送文本的应用程序。这将通过服务器发送,并且不会与电话(公司)本身有任何关系。检测并计算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