# search-dialog **Repository Path**: HarmonyOS-tpc/search-dialog ## Basic Information - **Project Name**: search-dialog - **Description**: An awesome and customizable search dialog with built-in search options. - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 4 - **Forks**: 2 - **Created**: 2021-04-15 - **Last Updated**: 2024-11-08 ## Categories & Tags **Categories**: harmonyos-popup **Tags**: None ## README # search-dialog An awesome and customizable search dialog with built-in search options. ## Show ## Usage 通过DevEco studio 2.0+,并下载SDK Native 版本2.0+ "在中文路径下,Build Debug Hap(s)会失败,请将项目放置在全英文目录下" 方式一: 在entry的gradle中集成如下操作: 修改implementation fileTree(dir: 'libs', include: ['*.jar','*.har'])增加*.har 方式二: ``` allprojects{ repositories{ mavenCentral() } } implementation 'io.openharmony.tpc.thirdlib:search-dialog:1.0.5' ``` ## Simple usage if you just want to use the simple search dialog first you need to provide searchable items. to achieve this you should implement Searchable in your model. you can see the SampleModel for example : ```java public class SampleSearchModel implements Searchable { private String mTitle; public SampleSearchModel(String title) { mTitle = title; } @Override public String getTitle() { return mTitle; } public SampleSearchModel setTitle(String title) { mTitle = title; return this; } } ``` now generate some search options in your ability : ```java private ArrayList createSampleData(){ ArrayList items = new ArrayList<>(); items.add(new SampleModel("First item")); items.add(new SampleModel("Second item")); items.add(new SampleModel("Third item")); items.add(new SampleModel("The ultimate item")); items.add(new SampleModel("Last item")); items.add(new SampleModel("Lorem ipsum")); items.add(new SampleModel("Dolor sit")); items.add(new SampleModel("Some random word")); items.add(new SampleModel("guess who's back")); return items; } ``` then you just need to add the below lines where you want to show the dialog : ```java new SimpleSearchDialogCompat(MainActivity.this, "Search...", "What are you looking for...?", null, createSampleData(), new SearchResultListener() { @Override public void onSelected(BaseSearchDialogCompat dialog, SampleModel item, int position) { // If filtering is enabled, [position] is the index of the item in the filtered result, not in the unfiltered source toast(item.getTitle()); dialog.hide(); } }).show(); ``` The constructor parameters are ```java SimpleSearchDialogCompat(Context context, String title, String searchHint, @Nullable Filter filter, ArrayList items, SearchResultListener searchResultListener) ``` ### Loading view added to SimpleSearchDialogCompat Just use `setLoading(true)` for showing and `setLoading(false)` for hiding it on an instance of SimpleSearchDialogCompat ## Advanced usage ### The layout I used this layout for simple search dialog but you can use anything else. Of course your layout should have thse two views : - An TextField to use as search key input - A ListContainer for showing the results in it ### The search dialog You can use your custom layouts, adapters and search options by creating a class inheriting the BaseSearchDialogCompat take a look at SimpleSearchDialogCompat to see an example of how it can be done You should implement the BaseSearchDialogCompat methods : ```java // handle your view with this one protected abstract void getView(Component view); // Id of your custom layout protected abstract int getLayoutResId(); // Id of the search TextField you used in your custom layout protected abstract int getSearchBoxId(); // Id of the ListContainer you used in your custom layout protected abstract int getRecyclerViewId(); ``` ### The search filter You can use your custom filters for text searching. The one used in SimpleSearchDialogCompat It checks the search key and if an item and the key had partially exact same letters it will add that item to results and also if the CheckLCS was set to true, it will check if the amount of matching letters was bigger than the given AccuracyPercentage the item will be added to results ### The adapter the one used in SimpleSearchDialogCompat is so simple despite its too long. the main functionality is in initializeViews method. you can create your custom adapters and use it instead of this one ### The StringsHelper it has two methods which you can use for highlighting the results. ```java /** * 给字体穿插上色 * * @param str1 列表中某一项字符串 * @param str2 搜索的字符 * @param defaultColor 默认的颜色 * @param highlightColor 红色 * @param textSize 字体大小 * @return 封装好的颜色字体 */ RichTextBuilder highlightLCS(String str1, String str2, int defaultColor, int highlightColor,int textSize); // Returns the LCS(Longest Common Subsequence) of two strings String lcs(String text1, String text2) ``` ## Entry运行要求 通过DevEco studio,并下载SDK 将项目中的build.gradle文件中dependencies→classpath版本改为对应的版本(即你的IDE新建项目中所用的版本) ## Licenses 版权所有2020-2025 根据Apache许可证2.0版(“许可证”)获得许可; 除非符合许可证,否则不得使用此文件。 您可以从以下网址获取许可证副本: http://www.apache.org/licenses/LICENSE-2.0 除非适用法律要求或书面同意,否则软件 根据许可证发放,按“原样”发放, 没有任何明示或暗示的保证或条件。 请参阅许可证以了解特定语言控制权限和 许可证的限制。