为什么使用预定义的 ID 资源设置 imeActionId 会产生错误? [英] Why does setting imeActionId with a predefined ID resource create an error?

查看:33
本文介绍了为什么使用预定义的 ID 资源设置 imeActionId 会产生错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Cyril Mottier 有一篇关于自定义 Android 软键盘上的发送/完成/返回键的精彩博文.在尝试代码时,我(以及评论中的其他几个人)注意到在 XML 中使用新 ID(例如 @+id/...)设置 imeActionId 会向 OnEditorActionListener 返回 0,当键被击中时用户,而不是唯一 ID.但是,如果您在 ids.xml 中设置 ID 并将 imeActionId 设置为该 ID(例如 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?

这是我所有代码的要点:https://gist.github.com/gsysko/d46adbe27d409bde0299

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

感谢您考虑这个问题.

推荐答案

原因是 imeActionId 在这种情况下有点用词不当.imeActionId 的 Javadoc 说:

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 使用的常量值混合在一起是不好的.Android 在解析您的 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"

在您的代码中,您可以检索它

In your code you can then retrieve it

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

好的,这引起了我的兴趣,所以进一步查看 Android 源代码,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);

如果找不到attr的int值,它将使用mEditor.mInputContentType.imeActionId作为默认值——在本例中为0,这就解释了为什么如果您使用新创建的 ID,它会返回 0.我还没有找到通货膨胀错误的原因.

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.

这篇关于为什么使用预定义的 ID 资源设置 imeActionId 会产生错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆