我正在为多种语言制作一个 Android 应用程序。我想根据键盘语言将 EditText View 的 inputType 设置为不同。在我试过的 XML 文件中:
android:inputType="@string/xyz"
在字符串资源文件中,字符串 xyz 将是
<string name="xyz">number</string>
我希望这是一个聪明的想法,可以根据语言轻松更改输入类型。例如,如果语言是英语,我会将 xyz 设置为字母,但在日语中,我会将 xyz 设置为数字。我试图避免以编程方式执行此操作,因为一旦我开始在代码中输入输入过滤器,我的大部分 XML 设置就会被忽略,即使它们没有被新代码直接覆盖。
但是当我尝试将此字符串资源调用到 inputType 时出现错误。如有任何帮助,我们将不胜感激。
最佳答案
是的,您可以通过编程方式实现这一点,因为 inputType
可以采用 integer
值。这是实现,它实际上非常简单。
TYPE_CLASS_TEXT有一个常数值:1
TYPE_CLASS_NUMBER具有常数值:2
这是您的 strings.xml 的值(您将对其进行本地化): 例如:
英文(文本)字符串中的 input_type 值:
<string name="input_type">1</string>
日语字符串中的 input_type 值(数字):
<string name="input_type">2</string>
在您的 Activity
中,声明您的 EditText
并设置 strings.xml 中的 inputType
,以便根据本地化进行设置:
editText = (EditText)findViewById(R.id.editText1);
editText.setInputType(Integer.parseInt(getResources().
getString(R.string.input_type)));
关于android - 是否可以使用android :inputType with a string resource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23363023/