我想检测用户在编辑文本(如印地语或英语)中键入哪种语言,因为我正在制作类似SMS应用程序的应用程序,而在这里,我正在制作一个文本计数器,该计数器根据edittext语言类型工作,即如果语言为English Counter显示160个字符或语言是印地语计数器显示60个字符。我可以在textwatcher的帮助下进行自定义计数器,但是在这里我无法检测到编辑文本的语言。
我尝试过:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
InputMethodSubtype ims = imm.getCurrentInputMethodSubtype();
String localeString = ims.getLocale();
Locale locale = new Locale(localeString);
String currentLanguage = locale.getDisplayLanguage();
但是在Logcat中什么也没给出。 而当我使用它时。
Log.d(TAG, "messageTYPE: " + Locale.getDefault().getDisplayLanguage());
每次进入印地语播种机时,它只会显示英语。
我想要什么?
如果我能够检测语言类型,那么可以为编辑文本创建一个自定义计数器。
OR
有什么方法可以使诸如Message App之类的编辑文本自动检测语言类型并根据语言设置计数器。
请帮助我,谢谢。
更新
if(s.length() < 160 || s.length() == 160){
if (s.length()/160 == 0) {
Log.d(TAG, "length160 " + s.length());
Log.d(TAG, "length160 " + (160-s.length()));
textViewCounter.setText((160 - s.length()) + "/" + "1");
messageCount =1;
Toast.makeText(SendingActivity.this, "length160: " + messageCount, Toast.LENGTH_SHORT).show();
}else if (s.length()/160 == 1){
textViewCounter.setText((160 - s.length()) + "/" + "1");
messageCount =1;
Toast.makeText(SendingActivity.this, "length1602: " + messageCount, Toast.LENGTH_SHORT).show();
}
}else if (s.length() > 160 && s.length() <= 305){
// textViewCounter.setText((305 - s.length()) + "/" + "2");
// messageCount = s.length()/160;
// Toast.makeText(SendingActivity.this, "above " + messageCount, Toast.LENGTH_SHORT).show();
if (s.length()/305 == 0) {
Log.d(TAG, "length305 " + s.length());
Log.d(TAG, "length305 " + (305-s.length()));
textViewCounter.setText((305 - s.length()) + "/" + "2");
messageCount = 2;
Toast.makeText(SendingActivity.this, "length305: " + messageCount, Toast.LENGTH_SHORT).show();
}else if (s.length()/305 == 1){
textViewCounter.setText((305 - s.length()) + "/" + "2");
messageCount = 2;
Toast.makeText(SendingActivity.this, "length3052: " + messageCount, Toast.LENGTH_SHORT).show();
}
}else if (s.length() > 305 && s.length() <= 459){
// textViewCounter.setText((305 - s.length()) + "/" + "2");
// messageCount = s.length()/160;
// Toast.makeText(SendingActivity.this, "above " + messageCount, Toast.LENGTH_SHORT).show();
if (s.length()/459 == 0) {
Log.d(TAG, "length459 " + s.length());
Log.d(TAG, "length459 " + (459-s.length()));
textViewCounter.setText((459 - s.length()) + "/" + "3");
messageCount = 3;
Toast.makeText(SendingActivity.this, "length459: " + messageCount, Toast.LENGTH_SHORT).show();
}else if (s.length()/305 == 1){
textViewCounter.setText((459 - s.length()) + "/" + "3");
messageCount = 3;
Toast.makeText(SendingActivity.this, "length4592: " + messageCount, Toast.LENGTH_SHORT).show();
}
}
这是用于计数单词并制作类似消息的应用程序的自定义计数器。
答案 0 :(得分:1)
使用SmsMessage.calculateLength()
(请参阅docs)来计算剩余字符数:
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int[] result = SmsMessage.calculateLength(s, false);
textViewCounter.setText("" + result[2] + "/" + result[0]);
messageCount = result[0];
Log.i("TAG", "Characters: " + result[1] + "/" + (result[1] + result[2]));
Log.i("TAG", "Number of SMS's: " + result[0]);
}
@Override
public void afterTextChanged(Editable s) {
}
});