为什么设置imeActionId了predefined ID资源创建一个错误?创建一个、错误、资源、imeActionId

2023-09-05 08:53:16 作者:温酒叙余生

西里尔Mottier对自定义发送对Android软键盘 /做/返回键一个伟大的职位。当尝试了code,我(和其他几个人在评论)注意到,设置imeActionId与XML一个新的ID(如@ + ID / ...)返回一个0到OnEditorActionListener的时候,关键是由用户,而不是唯一的ID击中。但是,如果设置在ids.xml ID和设置imeActionId到(如W / @id / ...)它会导致一个布局通胀的错误。

Cyril Mottier has a great post on customizing the send/done/return key on the Android soft keyboard. When trying out the code, I (and several others in the comments) noticed that setting the imeActionId with a new ID in XML (e.g. @+id/...) returns a 0 to the OnEditorActionListener, when the key is hit by the user, not the unique ID. However, if you set an ID in ids.xml and set imeActionId to that (e.g. w/ @id/...) it causes a layout inflation error.

我能顺利拿到imeActionId只有这样,才能被设置为一个唯一的ID是通过编程设置它在Java中。那么,什么是XML的正确使用属性imeActionId?

The only way I could successfully get the imeActionId to be set to a unique ID was to set it programmatically in Java. So what is the correct usage of the XML attribute imeActionId?

下面是我所有的code一个要点是:https://gist.github.com/gsysko/d46adbe27d409bde0299

Here is a Gist with all of my code: https://gist.github.com/gsysko/d46adbe27d409bde0299

谢谢您考虑这个问题。

推荐答案

原因是, imeActionId 在这种情况下,轻微的用词不当。在Javadoc中 imeActionId 说:

The reason is that imeActionId is a slight misnomer in this case. The Javadoc for imeActionId says:

供应用于EditorInfo.actionId一个值时的输入方法被连接到文本视图。

Supply a value for EditorInfo.actionId used when an input method is connected to the text view.

这是找您分配一个值。一个资源ID是用于识别资源在你的应用程序并没有保证值。在某些情况下,您可以根据资源的ID,如 View.getId()作比较,但它不是好混的资源ID在与 EditorInfo 使用。机器人可尝试prevent你这样做时,它在解析XML文件通过抛出异常就像你看到的,但有没有很多检查,它可以在运行时做,如果你编程设定。

It is looking for you to assign a value. A resource ID is for identifying resources in your app and does not have a guaranteed value. In some cases you can make comparisons based on resource ID's, such as View.getId(), but it is not good to mix resource ID's in with constant values that EditorInfo uses. Android may try to prevent you from doing this when it parses in your XML files by throwing exceptions like you saw, but there's not many checks it can do at runtime if you set it programmatically.

相反,你可以在你的资源,像这样定义一个整数值:

Instead, you can define an integer value in your resources like so:

<!--res/values/integers.xml-->
<resources>
<item type="integer" name="customImeActionId" format="integer">100</item>
</resources>

和使用它像

android:imeActionId="@integer/customImeActionId"

在你的code然后你可以检索

In your code you can then retrieve it

int imeActionId = getResources().getInteger(R.integer.customImeActionId);

编辑:好了,这激起了我的兴趣,让更多看在Android源$ C ​​$ C,TextView的分析类似的特性:

edit: OK, this has piqued my interest so looking further in the Android source code, TextView parses the attribute like:

mEditor.mInputContentType.imeActionId = a.getInt(attr, mEditor.mInputContentType.imeActionId);

这将使用 mEditor.mInputContentType.imeActionId 为默认值 - 这是0在这种情况下 - 如果它不能找到 ATTR ,这可以解释为什么它返回0,如果您使用的是新创建的ID。我还没有发现通胀错误的原因。

It will use mEditor.mInputContentType.imeActionId as the default value -- which is 0 in this case -- if it can't find the int value of attr, which explains why it returns 0 if you use a newly created ID. I haven't found the cause of the inflation error.