1. Add custom to the xml file Search view
< android: style="@style/StyleEditText" android:hint="Search for Attraction Information" />
2. Custom view java file
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class IconCenterEditText extends EditText implements , { private static final String TAG = (); /** * Is it the style on the left of the default icon? */ private boolean isLeft = false; /** * Whether to click on the soft keyboard to search */ private boolean pressSearch = false; /** * Soft keyboard search key monitoring */ private OnSearchClickListener listener; public void setOnSearchClickListener(OnSearchClickListener listener) { = listener; } public IconCenterEditText(Context context) { this(context, null); init(); } public IconCenterEditText(Context context, AttributeSet attrs) { this(context, attrs, ); 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 directly (canvas); } else { // If it is not the default style, you need to 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 = (); // float bodyWidth = textWidth + drawableWidth + drawablePadding; // // ((getWidth() - bodyWidth - getPaddingLeft() - getPaddingRight()) / 2, 0); // } // if (drawableRight != null) { // float textWidth = getPaint().measureText(getHint().toString()); // Text width// int drawablePadding = getCompoundDrawablePadding(); // Icon spacing// int drawableWidth = (); // Icon Width// float bodyWidth = textWidth + drawableWidth + drawablePadding; // setPadding(getPaddingLeft(), getPaddingTop(), (int)(getWidth() - bodyWidth - getPaddingLeft()), getPaddingBottom()); // ((getWidth() - bodyWidth - getPaddingLeft()) / 2, 0); // } (canvas); } } public void translate(Drawable drawable, Canvas canvas) { if (drawable != null) { float textWidth = getPaint().measureText(getHint().toString()); int drawablePadding = getCompoundDrawablePadding(); int drawableWidth = (); float bodyWidth = textWidth + drawableWidth + drawablePadding; if (drawable == getCompoundDrawables()[0]) { ((getWidth() - bodyWidth - getPaddingLeft() - getPaddingRight()) / 2, 0); } else { setPadding(getPaddingLeft(), getPaddingTop(), (int)(getWidth() - bodyWidth - getPaddingLeft()), getPaddingBottom()); ((getWidth() - bodyWidth - getPaddingLeft()) / 2, 0); } } } @Override public void onFocusChange(View v, boolean hasFocus) { (TAG, "onFocusChange execute"); // Restore the default style of EditText if (!pressSearch && (getText().toString())) { isLeft = hasFocus; } } @Override public boolean onKey(View v, int keyCode, KeyEvent event) { pressSearch = (keyCode == KeyEvent.KEYCODE_ENTER); if (pressSearch && listener != null) { /*Hidden soft keyboard*/ InputMethodManager imm = (InputMethodManager) ().getSystemService(Context.INPUT_METHOD_SERVICE); if (()) { ((), 0); } (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/grey</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:andro> <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(.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)) { ("Please enter what you want to search for"); } else { //Realize jumps based on your text content Intent intent = new Intent(context, ); // ("searchMode", 1); ("searchWord", texts); (intent); } } });
The above content is the Android custom View soft keyboard implementation that the editor introduced to you. I hope you like it.