EditText ignores the ImeActionLabel

2019-07-08 03:05发布

问题:

Using a Samsung Galaxy Ace, when i instantiate the editText i try and set a custom label that is being ignored at the moment (no third-party keyboard) same behaviour on a Samsung Galxy SII This is the code i'm using for setting the options

eTHomeShare = (EditText) v.findViewById(R.id.eTHomeShare);
eTHomeShare.setImeActionLabel(getString(R.string.home_done),
            EditorInfo.IME_ACTION_UNSPECIFIED);

This is the xml definition of the EditText

           <EditText 
                android:id="@+id/eTHomeShare"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:imeActionLabel="@string/done"
                android:imeOptions="actionSend"
                android:inputType="text" />

EDIT 1

New xml for editText as CommonsWare suggested

           <EditText 
                android:id="@+id/eTHomeShare"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical" />

As you can see i just removed the 2 lines from the xml, and in the onCreateView of my fragment i use the

 eTHomeShare.setImeActionLabel(getString(R.string.home_done),
            EditorInfo.IME_ACTION_SEND);

Now i cannot see the Done/Send button, (btw i'm always working on Portrait) but instead the Enter symbol, the same result if i remove the lines on initViews and let the original xml definition.

EDIT 2 Dumb mistake the line: android:inputType="text" was removed from the xml, just using the one on initViews worked fine at once.

回答1:

There are many ways to try to control what goes on the action button, including:

  • imeOptions
  • imeActionLabel
  • setImeActionLabel()

Which, if any, of those will work will depend upon the implementation of the input method editor (IME, a.k.a., soft keyboard). Ideally, each IME will honor all of them... at least, when used individually.

However:

  • The behavior when you specify it more than one is undefined

  • There is no requirement of any IME to even have an action button, let alone honor your request for a particular caption on that button

In general, I recommend specifying the action button label in one spot. My guess is that imeOptions is most likely to be honored, so if your desired label is one of the standard options (e.g., "send"), use that.