Android 如何在所有设备上为所有语言设置相同的EditText长度(宽度)?

Android 如何在所有设备上为所有语言设置相同的EditText长度(宽度)?,android,android-edittext,Android,Android Edittext,在我的应用程序中,我将EditText的最大长度设置为30。当我输入英语时,这很好。然而,当我输入中文时,我也可以输入30个字符。中文字符的宽度是英文字符的两倍。编辑文本的宽度也比我键入英语时的宽度增加了一倍。这不是我想要的 如何将30个英文字符设置为最大值?当用户使用其他语言键入时,EditText的宽度不能超过30个英文字符。谢谢您可以通过首先检测设备键盘的区域设置,然后从中获取区域设置对象,然后设置最大长度来获取键盘语言。比如说 InputMethodManager imm = (Inp

在我的应用程序中,我将EditText的最大长度设置为30。当我输入英语时,这很好。然而,当我输入中文时,我也可以输入30个字符。中文字符的宽度是英文字符的两倍。编辑文本的宽度也比我键入英语时的宽度增加了一倍。这不是我想要的


如何将30个英文字符设置为最大值?当用户使用其他语言键入时,EditText的宽度不能超过30个英文字符。谢谢

您可以通过首先检测设备键盘的区域设置,然后从中获取区域设置对象,然后设置最大长度来获取键盘语言。比如说

 InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    InputMethodSubtype ims = imm.getCurrentInputMethodSubtype();
    String localeString = ims.getLocale();
    Locale locale = new Locale(localeString);
    if( (locale.getDisplayLanguage()).equalIgnoreCase("english"))
      {
        editText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(30)});
       }
请尝试以下代码:

String language = Locale.getDefault().getDisplayLanguage().toString();

 if(language.equals(en) {

 // use set filter to limit input length in the edit text for english.
 int maxLength = 30;    
 editText.setFilters(new InputFilter[] {new 
 InputFilter.LengthFilter(maxLength)});

 } else {

// put the limit for other languages

}

希望对您有所帮助。

谢谢。如果我确切地知道我的目标语言,这将起作用。我知道汉字比英文大。然而,我不知道的其他语言可能与汉语相同。如果它适用于所有语言,那就太好了。