Simple usage of AutoCompleteTextView in Android (implementation of search history)

There are many tutorials on the Internet. The character set of the prompt box is written in advance. For example, a string [] array is used to contain these data. However, we can also save the data entered by the user as a historical record

Let's start with the code I wrote:

In the above code, I have implemented the function of autocomplettextview to read and display the history record from the sharereference. When there is no input, I will prompt the latest five history records (a condition can be added here, and it will be displayed only when there is a history record)

Fill in the code of the layout

When there is an EditText or auto, you will get the focus by default when entering the picture. To remove the focus, you can add an o-pixel layout before auto and set it to get the focus first.

The renderings are as follows

The following is the source code content

It should be noted that I use several methods of AutoCompleteTextView here

1. Setadapter () method: the adapter parameters to be passed here must inherit listadapter and filterable. Arrayadapter and simpleadapter can meet the requirements. We often use arrayadapter because it does not need to set its display position and textview components like simpleadapter.

If you want to master it, you must check its source code. We can see how arrayadapter is implemented

All adapters that inherit filterable must override the getfilter interface method

This filter is the object that implements the filtering method. Similarly, we can see how its source code is implemented

This is a private internal class customized by arrayadapter. The so-called private means that you cannot modify this filtering method through inheritance. Similarly, you cannot directly get the filtered result set results. If you want to use a new filter method, you must override the getfilter () method. The returned filter object is the filter object you want to create (including the performfiltering () method to reconstruct the filter method you want)

2. Setdropdownheight method is used to set the height of the prompt drop-down box. Note that this only limits the height of the prompt drop-down box, and the number of prompt data sets does not change

3. Setthreshold method, set the prompt from the input of the first few characters

4. Setcompletionhint method to set the text displayed at the bottom of the prompt box

5. Setonfocuschangelistener method, which contains onfocuschangelistener listener and sets focus change events

6. Use the showdropdown method to pop up the drop-down box

Some methods I didn't use are listed

1. Clearlistselection, which removes the selector style, is only temporarily removed and reappears when the user enters again

2. Disassdropdown, close the drop-down prompt box

3. Enoughtofilter. This is a method to check whether the filter conditions are met. SDK suggests that we can override this method

4. Getadapter to get a filterable list adapter

5. Getdropdownanchor to get the ID of the view of the anchor meter in the drop-down box

6. Getdropdownbackground to get the background color of the drop-down box

7. Setdropdownbackgrounddrawable to set the background color of the drop-down box

8. Setdropdownbackgroundresource to set the background resource of the drop-down box

9. Setdropdownverticaloffset, set the vertical offset of the drop-down list, that is, the number of data items contained in the list

10. Getdropdownverticaloffset to get the vertical offset of the drop-down list

11.. Setdropdownhorizontaloffset to set the horizontal offset

12. Setdropdownanimationstyle to set the pop-up animation of the drop-down box

13. Getthreshold to get the number of filtered characters

14. Setonitemclicklistener, set the drop-down box click event

15. Getlistselection to get the selected position of the drop-down box

16.getOnItemClickListener。 Get single click event

17. Getonitemselectedlistener gets the single selected event

18. Getadapter, get some hidden methods and structures of the set adapter. I haven't listed them. For details, please refer to the API document

Custom:

What you find on the Internet is used together with the arrayadapter. Sometimes you need to customize the style. What should I do? follow me!

In fact, to use AutoCompleteTextView, you have to implement the filter filterable. You have to tell it how to filter. Since the arrayadapter has implemented the filterable interface for us, we can easily ignore this and think that AutoCompleteTextView is very simple to use. What if you use baseadapter? Of course, in fact, it is not difficult, as long as it also implements the filterable interface.

Here is the source code:

Implement a custom adapter

simple_ list_ item_ for_ autocomplete. xml

Use, usually like this:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>