Allows you to search and customize a View on a keyboard.
1. Add Custom Search view to the xml file
<Com. etoury. etoury. ui. view. iconCenterEditText android: id = "@ + id/search_et" style = "@ style/StyleEditText" android: hint = ""/>
2. Custom view java File
IconCenterEditText. java
Package com. etoury. etoury. ui. view; import android. content. context; import android. graphics. canvas; import android. graphics. drawable. drawable; import android. text. textUtils; import android. util. attributeSet; import android. util. log; import android. view. keyEvent; import android. view. view; import android. view. inputmethod. inputMethodManager; import android. widget. editText; public class IconCenterEditText extends EditText implements View. onFocusChangeListener, View. onKeyListener {private static final String TAG = IconCenterEditText. class. getSimpleName ();/*** whether it is the default icon and then the style on the left */private boolean isLeft = false;/*** whether to click the keyboard to search */private boolean pressSearch = false; /*** soft keyboard search key listener */private OnSearchClickListener listener; public void setOnSearchClickListener (OnSearchClickListener listener) {this. listener = Listener;} public IconCenterEditText (Context context) {this (context, null); init ();} public IconCenterEditText (Context context, AttributeSet attrs) {this (context, attrs, android. r. attr. editTextStyle); init ();} public IconCenterEditText (Context context, AttributeSet attrs, int defStyleAttr) {super (context, attrs, defStyleAttr); init ();} private void init () {setOnFocusChangeListener (this ); SetOnKeyListener (this) ;}@ Override protected void onDraw (Canvas canvas) {if (isLeft) {// if it is the default style, draw super directly. onDraw (canvas);} else {// if not the default style, draw the icon in the middle Drawable [] drawables = getCompoundDrawables (); Drawable drawableLeft = drawables [0]; drawable drawableRight = drawables [2]; translate (drawableLeft, canvas); translate (drawableRight, canvas); // if (drawableLeft! = Null) {// float textWidth = getPaint (). measureText (getHint (). toString (); // int drawablePadding = getCompoundDrawablePadding (); // int drawableWidth = drawableLeft. getIntrinsicWidth (); // float bodyWidth = textWidth + drawableWidth + drawablePadding; // canvas. translate (getWidth ()-bodyWidth-getPaddingLeft ()-getPaddingRight ()/2, 0); //} // if (drawableRight! = Null) {// float textWidth = getPaint (). measureText (getHint (). toString (); // text width // int drawablePadding = getCompoundDrawablePadding (); // icon spacing // int drawableWidth = drawableRight. getIntrinsicWidth (); // icon width // float bodyWidth = textWidth + drawableWidth + drawablePadding; // setPadding (getPaddingLeft (), getPaddingTop (), (int) (getWidth () -bodyWidth-getPaddingLeft (), getPaddingBottom (); // canv As. translate (getWidth ()-bodyWidth-getPaddingLeft ()/2, 0); //} super. onDraw (canvas) ;}} public void translate (Drawable drawable, Canvas canvas) {if (drawable! = Null) {float textWidth = getPaint (). measureText (getHint (). toString (); int drawablePadding = getCompoundDrawablePadding (); int drawableWidth = drawable. getIntrinsicWidth (); float bodyWidth = textWidth + drawableWidth + drawablePadding; if (drawable = getCompoundDrawables () [0]) {canvas. translate (getWidth ()-bodyWidth-getPaddingLeft ()-getPaddingRight ()/2, 0);} else {setPadding (getPadd IngLeft (), getPaddingTop (), (int) (getWidth ()-bodyWidth-getPaddingLeft (), getPaddingBottom (); canvas. translate (getWidth ()-bodyWidth-getPaddingLeft ()/2, 0) ;}}@ Override public void onFocusChange (View v, boolean hasFocus) {Log. d (TAG, "onFocusChange execute"); // restore the default EditText style if (! PressSearch & TextUtils. isEmpty (getText (). toString () {isLeft = hasFocus ;}@ Override public boolean onKey (View v, int keyCode, KeyEvent event) {pressSearch = (keyCode = KeyEvent. KEYCODE_ENTER); if (pressSearch & listener! = Null) {/* Hide the soft keyboard */InputMethodManager imm = (InputMethodManager) v. getContext (). getSystemService (Context. INPUT_METHOD_SERVICE); if (imm. isActive () {imm. hideSoftInputFromWindow (v. getApplicationWindowToken (), 0);} listener. onSearchClick (v);} return false;} public interface OnSearchClickListener {void onSearchClick (View view );}}
3. style
</Style> <style name = "StyleEditText"> <item name = "android: layout_width"> match_parent </item> <item name = "android: layout_height "> wrap_content </item> <item name =" android: background "> @ drawable/bg_search_bar </item> <item name =" android: drawablePadding "> 5dp </item> <item name =" android: gravity "> center_vertical </item> <item name =" android: imeOptions "> actionSearch </item> <item name =" android: drawableLeft "> @ drawable/icon_search </item> <item name =" android: padding "> 5dp </item> <item name =" android: singleLine "> true </item> <item name =" android: textColorHint "> @ color/gray </item> <item name =" android: textSize "> 16sp </item> <item name =" android: hint "> Search </item> </style>
4. bg_search_bar.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="@android:color/white" /> <stroke android:width="1px" android:color="@android:color/darker_gray" /> <corners android:radius="3dp" /></shape>
5. Add code to the activity
private IconCenterEditText search_et;
search_et = (IconCenterEditText) findViewById(R.id.search_et);
Search_et.setOnSearchClickListener (new OnSearchClickListener () {@ Override public void onSearchClick (View view) {// TODO Auto-generated method stub String texts = search_et.getText (). toString (). trim (); if ("". equals (texts) {ToastUtil. showToast ("Enter the content you want to search for");} else {// jump based on your text content
Intent intent = new Intent(context, SearchWordActivity.class); // intent.putExtra("searchMode", 1); intent.putExtra("searchWord", texts); context.startActivity(intent); } } });