Android Practice Notes (1) --- ImeOption explanation of EditText attributes, androidedittext

Source: Internet
Author: User

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:







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.