In Android development, the search box is very commonly used, but the control is not ready-made and needs to be encapsulated by itself. How should we encapsulate it?
Method 1: use a combination of XML and JAVA code. Define related controls and la s for search in XML, and control corresponding events in JAVA code.
Method 2: Use SearchRecentSuggestionsProvider and searchable to implement the floating search box.
Method 3: Use JAVA code for implementation.
There are already a lot of online code in the previous two types. Here we use method 3 for implementation. Let's take a look.
Function: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + KDEpoaLL0cv3v/strong + Wz/strong + weight + 8rks/weight + 0sC + weight/weight + weight/8r/weight/WoaM8L3A + weight/ytcTM4cq + weight + KDQpoaLH5b/Vzbyx6r/release + KOs0tTP7NOmx + W/1bLZ1/release + release/y1bzC + release/rrNz + DTprXEwt + release + upload "brush ": java; "> package com. example. searchframetest; import android. content. context; import android. content. res. resources; import android. graphics. drawable. drawable; import android. text. editable; import android. text. inputType; import android. text. textUtils; import android. text. textWatcher; import android. util. attributeSet; import android. view. motionEvent; import android. view. view; import android. widget. button; import android. widget. editText; import android. widget. linearLayout; public class SearchWidget extends LinearLayout {public final static int SEARCH_ID = 0x7ff20001; public final static String HINT_NAME = "hint"; private EditText _ data_editText = null; private Button _ search_button = null; private Context _ context = null; private Drawable _ clear_drawable = null; private Drawable _ search_drawable = null; private Resources _ res = null; private AttributeSet _ attrs = null; private String _ hint = ""; public SearchWidget (Context context, AttributeSet attrs) {super (context, attrs); if (context = null) {return ;} _ context = context; _ attrs = attrs; Init ();} private void Init () {InitParams (); InitAttrs (); InitControls (); InitLayout (); bindingEvents ();} private void InitLayout () {this. setOrientation (LinearLayout. HORIZONTAL);} private void InitParams () {_ res = _ context. getResources (); _ clear_drawable = _ res. getDrawable (R. drawable. clear); _ search_drawable = _ res. getDrawable (R. drawable. search);} private void InitAttrs () {for (int I = 0; I <_ attrs. getAttributeCount (); I ++) {if (_ attrs. getAttributeName (I ). equals (HINT_NAME) {_ hint = _ attrs. getAttributeValue (I); break ;}} private void InitControls () {_ data_editText = new EditText (_ context); _ search_button = new Button (_ context ); layoutParams dataLayoutParams = new LayoutParams (0, LayoutParams. FILL_PARENT, 1); _ data_editText.setLayoutParams (dataLayoutParams); _ values (null, null, _ search_drawable, null); _ search_button.setId (SEARCH_ID); this. addView (_ data_editText); this. addView (_ search_button); // addHint (); _ data_editText.setHint (_ hint); // _ data_editText.setFocusable (false);} private void BindingEvents () {_ listener (_ search_TextChanged); _ listener (_ search_OnTouch);} public void setSearchOnClickListener (OnClickListener onclickListener) {_ search_button.setOnClickListener (onclickListener);} public String getSearchData () {return _ data_editText.getText (). toString ();} private TextWatcher _ search_TextChanged = new TextWatcher () {@ Overridepublic void afterTextChanged (Editable s) {Editable data = s; if (TextUtils. isEmpty (data) {_ partition (null, null); return;} _ data_editText.setCompoundDrawablesWithIntrinsicBounds (null, null, _ clear_drawable, null );} @ Overridepublic void beforeTextChanged (CharSequence s, int start, int count, int after) {}@ Overridepublic void onTextChanged (CharSequence s, int start, int before, int count) {// Log. e ("TEST", "3") ;}}; private OnTouchListener _ search_OnTouch = new OnTouchListener () {@ Overridepublic boolean onTouch (View v, MotionEvent event) {switch (event. getAction () {case MotionEvent. ACTION_UP: {int curX = (int) event. getX (); String data = _ data_editText.getText (). toString (); if (TextUtils. isEmpty (data) {return false;} boolean isClearPosition = (curX> v. getWidth ()-88); if (isClearPosition) {Clear (event); return true ;}}default: {break ;}return false;} private void Clear (MotionEvent event) {int cacheInputType = _ data_editText.getInputType (); // backup // the // input // type_data_editText.setInputType (InputType. TYPE_NULL); // disable // soft // invoke (event); // call native handler_data_editText.setInputType (cacheInputType); // restore input // type // addHint (); _ data_editText.setText ("");}};}Note:
(1) define an id for _ search_button to respond to click events. The SearchWidget. SEARCH_ID here may conflict with the ID value defined in xml, and can be adjusted accordingly.
(2) define HINT_NAME to use the hint attribute when calling the search box control in xml.
(3) In the InitAttrs method, the hint attribute is ignored.
(4), _ data_editText.setHint (_ hint.
(5) BindingEvents Add the _ data_editText file change and touch event listening.
(6) Add the getSearchData function for external calls.
(7) added the setSearchOnClickListener for listening events where the search button is set externally.
(8). setCompoundDrawablesWithIntrinsicBounds dynamically modifies the icon on the right of the input box.
(9) In layoutParams layout parameter settings of _ data_editText, set its width to 0, its height to full parent container, and its weight to 1, to ensure that the space outside the search button is filled.
Call code:
package com.example.searchframetest;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.TextView;public class MainActivity extends Activity {private SearchWidget _search_widget = null;private TextView _result_text=null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Init();}private void Init() {FetchUIControls();BindingEvents();}private void FetchUIControls() {_search_widget = (SearchWidget) findViewById(R.id.searchWidget);_result_text = (TextView) findViewById(R.id.result);}private void BindingEvents() {_search_widget.setSearchOnClickListener(_clickListener);}private OnClickListener _clickListener = new OnClickListener() {@Overridepublic void onClick(View v) {switch (v.getId()) {case SearchWidget.SEARCH_ID: {Search();break;}default: {break;}}}};protected void Search() {String data=_search_widget.getSearchData();_result_text.setText(data);}}Note:
(1) Use common methods to obtain the SearchWidget.
(2) When setting a listening event for the Search button, use SearchWidget. SEARCH_ID to differentiate the response ID of the click event.
Reprinted please indicate the source http://blog.csdn.net/xxdddail/article/details/23194573