Android实现搜索历史功能

Android实现搜索历史功能

2023年7月7日发(作者:)

Android实现搜索历史功能本⽂实例为⼤家分享了Android实现搜索历史的具体代码,供⼤家参考,具体内容如下SharedPreferences实现本地搜索历史功能,覆盖搜索重复的⽂本,可清空

1. 判断搜索内容是否含表情,不需要可以不判断/** * 校验字符串是否含有表情 * @param content * @return */ public static boolean hasEmoji(String content){

Pattern pattern = e("[ud83cudc00-ud83cudfff]|[ud83dudc00-ud83dudfff]|[u2600-u27ff]"); Matcher matcher = r(content); if(matcher .find()){ return true; } return false;}2.软键盘⼯具类弹出、关闭,不需要可以不判断public class KeyBoardUtils {

/** * 打开软键盘 * * @param editText * @param context */ public static void openKeybord(EditText editText, Context context) { InputMethodManager imm = (InputMethodManager) temService(_METHOD_SERVICE); ftInput(editText, _SHOWN); SoftInput(_FORCED, _IMPLICIT_ONLY); }

/** * 关闭软键盘 * @param editText * @param context */ public static void closeKeybord(EditText editText, Context context) { InputMethodManager imm = (InputMethodManager) temService(_METHOD_SERVICE); ftInputFromWindow(dowToken(), 0); } /** * 判断软键盘是否显⽰ * @param activity * @return */ public static boolean isSoftShowing(Activity activity) { //获取当前屏幕内容的⾼度 int screenHeight = dow().getDecorView().getHeight(); //获取View可见区域的bottom Rect rect = new Rect(); //DecorView即为activity的顶级view dow().getDecorView().getWindowVisibleDisplayFrame(rect); //考虑到虚拟导航栏的情况(虚拟导航栏情况下:screenHeight = + 虚拟导航栏⾼度) //选取screenHeight*2/3进⾏判断 return screenHeight*2/3 > ; } public static void hintKeyboard(Activity activity) { InputMethodManager imm = (InputMethodManager) temService(_METHOD_SERVICE); if (ve() && rentFocus() != null) { if (rentFocus().getWindowToken() != null) { ftInputFromWindow(rentFocus().getWindowToken(), _NOT_ALWAYS); }

} } /** * 打开软键盘 */ public static void openKeyboard(Handler mHandler, int s, final Activity activity) { layed(new Runnable() { @Override public void run() { InputMethodManager imm = (InputMethodManager) temService(_METHOD_SERVICE); SoftInput(0, _NOT_ALWAYS); } }, s); }

/** * 点击空⽩处关闭软键盘 */

public static void inputClose(View view, Context context) { if (view instanceof EditText) { ocus(); } try { InputMethodManager im = (InputMethodManager) temService(_METHOD_SERVICE); ftInputFromWindow(dowToken(), 0); } catch (NullPointerException e) { tackTrace(); } }

}3.存储⼯具类import Api;import t;import Preferences;import ;

/** * @author Administrator * SharedPreferences使⽤⼯具类 */@TargetApi(N_BREAD)public class SPUtils { private static SharedPreferences sp; private static SPUtils instance = new SPUtils(); public static Context mContext;

/** * 保存在⼿机⾥⾯的⽂件名 */ public static final String FILE_NAME = "maigoo";

private SPUtils() { }

/** * xxx改为你想保存的sp⽂件名称 */ public static SPUtils getInstance(Context context) { mContext = context; if (sp == null) { sp = licationContext().getSharedPreferences(FILE_NAME, _PRIVATE); } return instance; }

/** * 保存数据 */ public void put(String key, Object value) { if (value instanceof Integer) { ().putInt(key, (Integer) value).apply(); } else if (value instanceof String) { ().putString(key, (String) value).apply(); } else if (value instanceof Boolean) { ().putBoolean(key, (Boolean) value).apply(); } else if (value instanceof Float) { ().putFloat(key, (Float) value).apply(); } else if (value instanceof Long) { ().putLong(key, (Long) value).apply(); } }

/** * 2. 读取数据 */ public int getInt(String key, int defValue) { return (key, defValue); }

public String getString(String key, String defValue) { return ing(key, defValue); }

public boolean getBoolean(String key, boolean defValue) { return lean(key, defValue); }

/** * 读取数据 * * @param key * @param defValue * @return */ public T get(String key, T defValue) { T t = null; if (defValue instanceof String || defValue == null) { String value = ing(key, (String) defValue); t = (T) value; } else if (defValue instanceof Integer) { Integer value = (key, (Integer) defValue); t = (T) value; } else if (defValue instanceof Boolean) { Boolean value = lean(key, (Boolean) defValue); t = (T) value; } else if (defValue instanceof Float) { Float value = at(key, (Float) defValue); t = (T) value; } return t; }

/** * 保存搜索记录 * * @param keyword */ public void save(String keyword) { // 获取搜索框信息 SharedPreferences mysp = redPreferences("search_history", 0); String old_text = ing("history", ""); // 利⽤新增内容,逗号便于读取内容时⽤逗号拆分开 StringBuilder builder = new StringBuilder(old_text); (keyword + ",");

// 判断搜索内容是否已经存在于历史⽂件,已存在则不重复添加 if (!old_ns(keyword + ",")) { myeditor = (); ing("history", ng()); (); } }

public String[] getHistoryList() { // 获取搜索记录⽂件内容 SharedPreferences sp = redPreferences("search_history", 0); String history = ing("history", ""); // ⽤逗号分割内容返回数组 String[] history_arr = (","); // 保留前50条数据 if (history_ > 50) { String[] newArrays = new String[50]; opy(history_arr, 0, newArrays, 0, 50); } return history_arr; }

/** * 清除搜索记录 */ public void cleanHistory() { SharedPreferences sp = redPreferences("search_history", 0); editor = (); (); (); }}ty主要功能实现import patActivity;import ;import ;import oup;import ;import xt;import ew;

import rdUtils;import rUtils;import s;

public class Main2Activity extends AppCompatActivity { ZFlowLayout historyFl; EditText autoSearch; Button button_search;

@Override protected void onCreate(Bundle savedInstanceState) { te(savedInstanceState); setContentView(ty_main2); historyFl = findViewById(y_fl); autoSearch=findViewById(arch); button_search=findViewById(_search); initHistory(); button_lickListener(new kListener() { @Override public void onClick(View v) { if (Showing()) { yboard(); } String searchKey = t().toString(); if (!isNullorEmpty(searchKey)) { if (ji(t().toString())) { //含有⾮法字符串 } else { //搜索 String keyWord = t().toString(); if (!isNullorEmpty(keyWord)) { tance().save(t().toString()); } initHistory(); } } else { //搜索为空 }

} });

}

private boolean isNullorEmpty(String str) { return str == null || "".equals(str); } /** * 初始化 历史记录列表 */ private void initHistory() {

final String[] data = tance().getHistoryList(); LayoutParams layoutParams = new LayoutParams(_CONTENT, _CONTENT); gins(10, 10, 10, 10);

AllViews(); for (int i = 0; i < ; i++) { if (isNullorEmpty(data[i])) {

return; } //有数据往下⾛ final int j = i; //添加分类块 View paramItemView = getLayoutInflater().inflate(r_search_keyword, null); TextView keyWordTv = ewById(_content); t(data[j]); w(paramItemView, layoutParams);

lickListener(new kListener() { @Override public void onClick(View v) { if (Showing()) { yboard(); } t(data[j]); ection(data[j].length());//光标在最后 if (!isNullorEmpty(data[j])) { tance().save(t().toString()); } //点击事件 } }); // initautoSearch(); } }}5.布局⽂件activity_main2 adapter_search_keyword

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信