Detailed Explanation of How to Realize Search Box by Android Using EditText

  • 2021-09-12 02:25:45
  • OfStack

Introduction to EditText:

ED (short for EditText) is also a control frequently used in development. It is also an important component. It can be said that it is a window for users to transmit data with applications. For example, to realize a login interface, users need to enter an account number and password, and then our developers get the content input by users and submit it to the service area for judgment before making corresponding processing.

Introduction

The new app has the search function needs to be realized, is an input box, input text after the keyboard out search words, and then click search for network requests. Before falling into the pit, I tried to use searchview to achieve it, but it was really a pit in the custom style. Finally, I found that EditText can be achieved, and I was drunk.

Custom Styles for SearchView

I have only achieved part of the custom, the input box where the bottom of the blue line really do not know how to remove, there is a great God can advise 1.


int search_mag_icon_id = sv_search.getContext().getResources().getIdentifier("android:id/search_mag_icon", null, null);
ImageView search_mag_icon = (ImageView)sv_search.findViewById(search_mag_icon_id);// Get search icon 
search_mag_icon.setImageResource(R.drawable.search_icon);// Replace the resources of the icon. 

In fact, it is to find the resource file of searchView and replace it.

Similarly, the close icon can also be replaced, as long as it is modified "android:id/search_mag_icon" 为"android:id/search_close_btn" That's enough.

Font color size of text box, etc. "android:id/search_src_text"

Don't ask me how to know, it's written in searchview source code. It should be noted that when modifying the color and size of the text box, it is necessary to change view cast to textview or edittext. This box in the configuration file of the source code is an view, which can be turned into one.

EditText realizes SearchView function

In front of searchview, if you want to change the keyboard line feed to search, you need to modify the attributes of imeOptions, while the attributes of Edittext actually have this, so add it in the layout of edittext imeOptions = “actionSearch” But simply adding this property is not possible.


 <EditText 
    android:inputType="text" 
    android:singleLine="true" 
    android:imeOptions="actionSearch" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" />

Other basic attributes can be added at will, and then run app to find that after the input text, the newline is really replaced by search. None of the first three attributes can be less. In this case, we only need to add a search click event:


et_earch.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
@Override 
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {    
 if(actionId == EditorInfo.IME_ACTION_SEARCH){  
// If actionId It is searched id Then proceed to the following 1 Step operation   
  doSomething() }  
 return false; }
});

Implement the onEditorAction method.

The rest is different from searchView, that is, there is no close button. In fact, it is enough to listen to the input of edittext once, but I didn't do it later. addTextChangedListener should be realized. The idea is: if there is a problem in edittext, button of close will be displayed, and if there is no text, gone will drop close button in the layout.

Summarize


Related articles: