In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "how to achieve simple dynamic search function in Android". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Preface
When it comes to dynamic search for Android, most people should think of EditText's text change listener (addTextChangedListener). This article will briefly introduce it, but this article will introduce the implementation of SearchView+Listview.
Effect picture:
1. AddTextChangedListener
The brief idea of using this way is that when listening for text changes, use a Runnable of Handler post to make corresponding changes and dynamically modify the display of ListView.
Second, this article case 1. Introduce some methods of SearchView
SetIconified (): sets the search box to expand the display directly. Magnifying glass on the left (in the search box) there is a fork on the right to close the search box
SetIconifiedByDefault (): sets the search box to expand the display directly. There is a magnifying glass on the left (outside the search box) there is no X style click button on the right side there is an X style click button after the input button can not close the search box
OnActionViewExpanded (): sets the search box to expand the display directly. There is no magnifying glass on the left (in the search box). There is no fork on the right and there is an X-style click button after the input. The search box cannot be closed.
SetOnQueryTextListener (): sets the listener for user actions in SearchView.
SetSubmitButtonEnabled (): enables the show submit button when the query is not empty.
SetQueryHint (): query prompt statement
two。 Prepare data
This case uses a String array
Private final String [] mStrings = Cheeses.sCheeseStrings;3. Initialize and populate data mSearchView = (SearchView) findViewById (R.id.search_view); mListView = (ListView) findViewById (R.id.list_view); mListView.setAdapter (mAdapter = new ArrayAdapter (this, android.R.layout.simple_list_item_1, mStrings)) / / sets whether the unwanted options can be filtered out by the characters entered on the keyboard and navigated to the desired options. MListView.setTextFilterEnabled (true); setupSearchView (); private void setupSearchView () {/ / set the search box to expand the display directly. There is a magnifying glass on the left (in the search box) there is a fork on the right to close the search box / / mSearchView.setIconified (false); / / set the search box to expand and display directly. There is a magnifying glass on the left (outside the search box) there is no fork on the right and the fork after the input cannot close the search box / / mSearchView.setIconifiedByDefault (false); / / set the search box to expand and display directly. With or without magnifying glass on the left (in the search box), no fork on the right cannot close the search box mSearchView.onActionViewExpanded () after input; / / set listeners for user actions in SearchView. MSearchView.setOnQueryTextListener (this); / / enable the show submit button when the query is not empty. MSearchView.setSubmitButtonEnabled (false); / / query prompt statement mSearchView.setQueryHint (getString (R.string.cheese_hunt_hint));} 4. Fire the method public boolean onQueryTextChange (String newText) {if (TextUtils.isEmpty (newText)) {mListView.clearTextFilter ();} else {mListView.setFilterText (newText.toString ());} return true;} when writing simple logic / / user input characters in SearchView
JimengSearchView.java
Public class JimengSearchView extends Activity implements SearchView.OnQueryTextListener {private SearchView mSearchView; private ListView mListView; private ArrayAdapter mAdapter; private final String [] mStrings = Cheeses.sCheeseStrings; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); getWindow () .requestFeature (Window.FEATURE_ACTION_BAR); setContentView (R.layout.searchview_filter); mSearchView = (SearchView) findViewById (R.id.search_view) MListView = (ListView) findViewById (R.id.list_view); mListView.setAdapter (mAdapter = new ArrayAdapter (this, android.R.layout.simple_list_item_1, mStrings)); / / sets whether you can filter out unwanted options by typing characters on the keyboard and navigate to the desired options. MListView.setTextFilterEnabled (true); setupSearchView (); mListView.setOnItemClickListener (new AdapterView.OnItemClickListener () {@ Override public void onItemClick (AdapterView parent, View view, int position, long id) {String str = (String) ((TextView) view). GetText (); Toast.makeText (JimengSearchView.this,str,Toast.LENGTH_SHORT). Show ();}}) } private void setupSearchView () {/ / sets the search box to expand the display directly. There is a magnifying glass on the left (in the search box) there is a fork on the right to close the search box / / mSearchView.setIconified (false); / / set the search box to expand and display directly. There is a magnifying glass on the left (outside the search box) there is no fork on the right and the fork after the input cannot close the search box / / mSearchView.setIconifiedByDefault (false); / / set the search box to expand and display directly. With or without magnifying glass on the left (in the search box), no fork on the right cannot close the search box mSearchView.onActionViewExpanded () after input; / / set listeners for user actions in SearchView. MSearchView.setOnQueryTextListener (this); / / enable the show submit button when the query is not empty. MSearchView.setSubmitButtonEnabled (false); / / query prompt statement mSearchView.setQueryHint (getString (R.string.cheese_hunt_hint));} / / the method public boolean onQueryTextChange (String newText) {if (TextUtils.isEmpty (newText)) {mListView.clearTextFilter ();} else {mListView.setFilterText (newText.toString ()) is fired when the user enters a character } return true;} public boolean onQueryTextSubmit (String query) {return false;}}
Layout file:
Strings.xml
Please enter the content to be queried
Cheeses.java
Public class Cheeses {public static final String [] sCheeseStrings = {"Android Custom view 3D Cube", "Don't eat Fish", "Android Custom view using drawArc method to achieve dynamic effect", "Android 3D effect realization", "OkHttp Source Code Analysis", "Android Flip Animation (Card Flip effect)", "Android Custom view go Animation" "Android custom view imitating login interface text input box (Huawei Cloud APP)", "Android custom view tai chi map", "Android custom view acquisition of custom colors in attr", "Android anti-decompilation", "room additions, deletions and modifications commonly used by Android (external database)", "Android draws a line chart with Canvas" And simply encapsulated ","Android draws a real running lantern with Canvas", "implementation of Android network novel reader", "Android eye protection mode (argb)", "Android constrained layout ConstraintLayout", "Android realizes the jitter effect of EditText"} } this is the end of the content of "how to achieve simple dynamic search function in Android". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.