Today, I used a special EditText in the project. When the user enters content in the EditText and clicks the search button, the input content can be highlighted and added to the input container. When deleting, you can delete the keywords in the container one by one. Attached code:
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; /** * Created by Jackie on 2017/2/21. * EditText for search */ public class SearchEditText extends RelativeLayout { private Context mContext; private LayoutInflater mInflater; private View mView; private LinearLayout mContainer; private EditText mEditText = null; public SearchEditText(Context context) { this(context, null); } public SearchEditText(Context context, AttributeSet attrs) { this(context, attrs, 0); } public SearchEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); } private OnSearchChangeListener mSearchChangeListener; public interface OnSearchChangeListener { void searchChange(String s); void removeView(int position); } public void setOnSearchChangeListener(OnSearchChangeListener searchChangeListener) { mSearchChangeListener = searchChangeListener; } private void init(Context context) { mContext = context; mInflater = (mContext); mView = (.search_edittext_layout, null); LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); = 15; = 15; addView(mView, params); mContainer = (LinearLayout) (); mEditText = (EditText) (); (new OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_DEL) { if (isNotFastClick()) { if (().toString().length() > 0) { String str = ().toString(); str = (0, () - 1); (str); (()); } else { if (() > 0) { if (mSearchChangeListener != null) { (() - 1); } (() - 1); } } } } return true; } }); (new () { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_SEARCH) { if (().toString().trim().equals("")) { return true; } TextView textView = new TextView(mContext); (().toString().trim()); (14); (("#dfe0e0")); (10, 0, 10, 0); (.shape_edittext_round_bg); (); params = new (.WRAP_CONTENT, .WRAP_CONTENT); = 10; (params); if (mSearchChangeListener != null) { (().toString().trim()); } (""); (textView); } return true; } }); } public EditText getEditText() { return mEditText; } public LinearLayout getContainer() { return mContainer; } long lastClickTime = 0; public boolean isNotFastClick() { long time = (); if (time - lastClickTime >= 300) { lastClickTime = time; return true; } else { return false; } } }
search_edittext_layout.xml
<?xml version="1.0" encoding="utf-8"?> <HorizontalScrollView xmlns:andro android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="none" android:fillViewport="true" > <LinearLayout android:layout_width="match_parent" android:layout_height="33dp" android:orientation="horizontal"> <LinearLayout android: android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="horizontal" android:gravity="center_vertical" android:layout_gravity="center_vertical" /> <EditText android:gravity="center_vertical" android:layout_gravity="center_vertical" android: android:layout_width="wrap_content" android:layout_height="match_parent" android:background="@null" android:textSize="16dp" android:textColor="#dfe0e0" android:layout_weight="1" android:minWidth="50dp" android:imeOptions="actionSearch" android:singleLine="true" android:layout_marginLeft="10dp"/> </LinearLayout> </HorizontalScrollView>
shape_edittext_round_bg.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:andro android:shape="rectangle"> <solid android:color="#666666" /> <corners android:radius="10dp"/> </shape>
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.