Android Practice Notes (1) --- ImeOption explanation of EditText attributes, androidedittext
Android Practice Notes (1) --- ImeOption explanation of EditText attributes
1) Why is this attribute used?
A: When we enter the UI controls for text input, such as EditText
Enter in the lower right corner! You have not heard the error. This attribute is about the Enter key !!!
You can use the ImeOption attribute to set the content displayed by the enter key and listen for events ~
2) how to modify the content displayed by the Enter key:
A: You can directly set the ImeOption attribute. In addition, different results may occur when you use a third-party input method,
Or it hasn't changed. Here we only use the sogou input method as a reference, and the rest will be ignored!
You only need to add the ImeOption attribute to EditText! In addition, if you need to set it in Java code, call
The setImeOption method, and then the actionSearch parameter only needs to be changed to: EditorInfo. IME_ACTION_SEARCH.
And so on
<EditText android: id = "@ + id/editsearch" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: imeOptions = "actionSearch" android: text = "actionSearch, query" android: singleLine = "true"/>
The test result is as follows:
① Original input method/sogou Input Method
ActionUnspecified:~~~
ActionNone:~~~
ActionGo :~~~
ActionSearch :~~~
ActionSend :~~~
ActionNext :~~~
ActionDone :~~~
!!! Another problem is that if your system version is higher than 2.3, it is possible that this attribute is set, but it has no effect;
You only need to set singleLine to true or inputType to text for EditText !!!
3) capture the Click Event of the Enter key
This is also very simple. You only need to set the event listener for EditText: setoneditexceptionlistener, and then
Override the oneditexception (TextView v, int actionId, KeyEvent event) method,
Then, according to your needs, the corresponding status of the Enter key responds to the corresponding event. The sample code is as follows:
Note that pressing Enter will trigger this event!
Public oneditexceptionlistener editexceptionlistener = new oneditexceptionlistener () {@ Overridepublic boolean oneditexception (TextView v, int actionId, KeyEvent event) {if (actionId = EditorInfo. IME_ACTION_UNSPECIFIED) {Toast. makeText (getApplicationContext (), "No effect", Toast. LENGTH_SHORT ). show ();} else if (actionId = EditorInfo. IME_ACTION_SEARCH) {Toast. makeText (getApplicationContext (), "query", Toast. LENGTH_SHORT ). show () ;}return false ;}};
4) download the test demo:
Go back and give it back ~~
As follows: