Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

android - Why does setting imeActionId with a predefined ID resource create an error?

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.

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?

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

Thanks for considering this question.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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>

and use it like

android:imeActionId="@integer/customImeActionId"

In your code you can then retrieve it

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

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);

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...