如何在Android中使用EditText实现批量搜索关键词
如何在Android中使用EditText实现批量搜索关键词?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
十载的庆城网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。全网营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整庆城建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。成都创新互联从事“庆城网站设计”,“庆城网站推广”以来,每个客户项目都认真落实执行。
当用户在EditText中输入内容,点击搜索按钮的时候,输入的内容能够高亮,然后添加到输入的容器中。删除的时候,能够将容器中的关键词逐一删除。附上代码:
SearchEditText.java
package com.jackie.searchresultedittext; import android.content.Context; import android.graphics.Color; import android.util.AttributeSet; import android.view.Gravity; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.view.inputmethod.EditorInfo; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView; /** * Created by Jackie on 2017/2/21. * 用于搜索的EditText */ 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 = LayoutInflater.from(mContext); mView = mInflater.inflate(R.layout.search_edittext_layout, null); LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); params.leftMargin = 15; params.rightMargin = 15; addView(mView, params); mContainer = (LinearLayout) mView.findViewById(R.id.layout); mEditText = (EditText) mView.findViewById(R.id.edittext); mEditText.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_DEL) { if (isNotFastClick()) { if (mEditText.getText().toString().length() > 0) { String str = mEditText.getText().toString(); str = str.substring(0, str.length() - 1); mEditText.setText(str); mEditText.setSelection(str.length()); } else { if (mContainer.getChildCount() > 0) { if (mSearchChangeListener != null) { mSearchChangeListener.removeView(mContainer.getChildCount() - 1); } mContainer.removeViewAt(mContainer.getChildCount() - 1); } } } } return true; } }); mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_SEARCH) { if (mEditText.getText().toString().trim().equals("")) { return true; } TextView textView = new TextView(mContext); textView.setText(mEditText.getText().toString().trim()); textView.setTextSize(14); textView.setTextColor(Color.parseColor("#dfe0e0")); textView.setPadding(10, 0, 10, 0); textView.setBackgroundResource(R.drawable.shape_edittext_round_bg); textView.setGravity(Gravity.CENTER); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); params.leftMargin = 10; textView.setLayoutParams(params); if (mSearchChangeListener != null) { mSearchChangeListener.searchChange(mEditText.getText().toString().trim()); } mEditText.setText(""); mContainer.addView(textView); } return true; } }); } public EditText getEditText() { return mEditText; } public LinearLayout getContainer() { return mContainer; } long lastClickTime = 0; public boolean isNotFastClick() { long time = System.currentTimeMillis(); if (time - lastClickTime >= 300) { lastClickTime = time; return true; } else { return false; } } }
search_edittext_layout.xml
<?xml version="1.0" encoding="utf-8"?>
shape_edittext_round_bg.xml
<?xml version="1.0" encoding="utf-8"?>
看完上述内容,你们掌握如何在Android中使用EditText实现批量搜索关键词的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!
本文标题:如何在Android中使用EditText实现批量搜索关键词
本文路径:http://scyanting.com/article/giijdc.html