我正在设计一个英文和中文的Android应用程序。我想知道用户是否输入英文文本或中文文本?有没有办法在android中检查这个?
答案 0 :(得分:8)
如果您想检测输入字符串是否包含类似中文的字符(CJK),以下内容可能会对您有所帮助:
public static boolean isCJK(String str){
int length = str.length();
for (int i = 0; i < length; i++){
char ch = str.charAt(i);
Character.UnicodeBlock block = Character.UnicodeBlock.of(ch);
if (Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS.equals(block)||
Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS.equals(block)||
Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A.equals(block)){
return true;
}
}
return false;
}
答案 1 :(得分:2)
如果您想获取设备的默认语言Locale.getDisplayLanguage()应该为您提供用户的语言。
否则,this可能会对您有所帮助。
修改强>
我不确定,但谷歌翻译会这样做。当用户输入内容时,它会自动检测语言。因此,Google Translate API应该能够为您完成此任务。
<强> EDIT2 强>
是的,它使用简单的HttpGet,here是链接。
答案 2 :(得分:2)
接受的答案是不完整或过时的。以下是一些可用于测试角色是否为CJK表意文字的方法。我更全面的答案是here。
最好使用代码点而不是charAt
(如在接受的答案中),因为许多中文字符位于更高的代码平面中。使用charAt
只会给你一个代理对而不是实际的汉字。因此循环String的更好方法就是this:
final int length = myString.length();
for (int offset = 0; offset < length; ) {
final int codepoint = Character.codePointAt(myString, offset);
// use codepoint here
offset += Character.charCount(codepoint);
}
测试代码点可以通过以下方式之一完成。
private boolean isCJK(int codepoint) {
Character.UnicodeBlock block = Character.UnicodeBlock.of(codepoint);
return (Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS.equals(block)||
Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A.equals(block) ||
Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B.equals(block) ||
Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_C.equals(block) || // api 19, remove these if supporting lower versions
Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_D.equals(block) || // api 19
Character.UnicodeBlock.CJK_COMPATIBILITY.equals(block) ||
Character.UnicodeBlock.CJK_COMPATIBILITY_FORMS.equals(block) ||
Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS.equals(block) ||
Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS_SUPPLEMENT.equals(block) ||
Character.UnicodeBlock.CJK_RADICALS_SUPPLEMENT.equals(block) ||
Character.UnicodeBlock.CJK_STROKES.equals(block) || // api 19
Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION.equals(block) ||
Character.UnicodeBlock.ENCLOSED_CJK_LETTERS_AND_MONTHS.equals(block) ||
Character.UnicodeBlock.ENCLOSED_IDEOGRAPHIC_SUPPLEMENT.equals(block) || // api 19
Character.UnicodeBlock.KANGXI_RADICALS.equals(block) ||
Character.UnicodeBlock.IDEOGRAPHIC_DESCRIPTION_CHARACTERS.equals(block));
}
或API 19
private boolean isCJK(int codepoint) {
return Character.isIdeographic(codepoint);
}
或者对于API 24
private boolean isCJK(int codepoint) {
return (Character.UnicodeScript.of(codepoint) == Character.UnicodeScript.HAN);
}
答案 3 :(得分:1)
String language = Locale.getDefault().getDisplayLanguage(); will give your default language of your device
System.out.println("My locale::"+Locale.getDefault().getDisplayLanguage());
it will print like My locale:: english;
现在你必须检查
if(language.equalignorecase("engish")){
// do your stuff for english
}else{
// do your stuff for chienese
}