鸿蒙开源组件——基于鸿蒙风格的搜索控件

jacksky
发布于 2021-9-22 18:54
浏览
1收藏

SimpleSearchView

项目介绍

  • 项目名称:SimpleSearchView
  • 所属系列:openharmony 第三方组件适配移植
  • 功能:一款简单的基于鸿蒙风格的搜索控件
  • 项目移植状态:主功能完成
  • 调用差异:无
  • 开发版本:sdk6,DevEco Studio2.2 beta1
  • 基线版本:Release 0.1.6

效果演示鸿蒙开源组件——基于鸿蒙风格的搜索控件-鸿蒙开发者社区

 

鸿蒙开源组件——基于鸿蒙风格的搜索控件-鸿蒙开发者社区

安装教程

在moudle级别下的build.gradle文件中添加依赖

// 添加maven仓库
repositories {
   maven {
       url 'https://s01.oss.sonatype.org/content/repositories/releases/'
   }
}

// 添加依赖库
dependencies {
   implementation 'com.gitee.chinasoft_ohos:simplesearchview:1.0.1'   
}

在sdk6,DevEco Studio2.2 beta1下项目可直接运行 如无法运行,删除项目.gradle,.idea,build,gradle,build.gradle文件, 并依据自己的版本创建新项目,将新项目的对应文件复制到根目录下

使用说明

添加SimpleSearchView到你的DirectionalLayout

<StackLayout
    ohos:id="$+id:toolbar_container"
    ohos:height="match_content"
    ohos:width="match_parent"
    ohos:background_element="$color:colorPrimary">

    <DependentLayout
        ohos:height="match_parent"
        ohos:width="match_content">

        <Text
            ohos:id="$+id:txt_title"
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:italic="false"
            ohos:left_margin="10vp"
            ohos:text="$string:title"
            ohos:text_color="$color:white"
            ohos:text_size="20fp"
            ohos:top_margin="5vp"/>

        <Text
            ohos:id="$+id:txt_subtitle"
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:below="$+id:txt_title"
            ohos:bottom_margin="3vp"
            ohos:left_margin="10vp"
            ohos:text="$string:sub_title"
            ohos:text_color="$color:white"
            ohos:text_size="16fp"
            ohos:top_margin="2vp"/>

        <Image
            ohos:id="$+id:img_search"
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:align_parent_right="true"
            ohos:image_src="$graphic:ic_search_black_24dp"
            ohos:right_margin="50vp"
            ohos:vertical_center="true"/>

        <DirectionalLayout
            ohos:id="$+id:dl_menu"
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:align_parent_right="true"
            ohos:orientation="vertical"
            ohos:right_margin="20vp"
            ohos:vertical_center="true">

            <Image
                ohos:height="5vp"
                ohos:width="5vp"
                ohos:image_src="$graphic:ic_menu"/>

            <Image
                ohos:height="5vp"
                ohos:width="5vp"
                ohos:bottom_margin="1vp"
                ohos:image_src="$graphic:ic_menu"
                ohos:top_margin="1vp"/>

            <Image
                ohos:height="5vp"
                ohos:width="5vp"
                ohos:image_src="$graphic:ic_menu"/>

        </DirectionalLayout>

        <Text
            ohos:id="$+id:txt_menu"
            ohos:height="50vp"
            ohos:width="140vp"
            ohos:align_parent_right="true"
            ohos:background_element="$color:white"
            ohos:focusable="focus_enable"
            ohos:focusable_in_touch="true"
            ohos:layout_alignment="vertical_center"
            ohos:left_margin="15vp"
            ohos:left_padding="20vp"
            ohos:right_margin="5vp"
            ohos:text="Example"
            ohos:text_color="$color:default_textColor"
            ohos:text_size="18fp"
            ohos:top_margin="5vp"
            ohos:visibility="invisible"/>
    </DependentLayout>

    <com.ferfalk.simplesearchview.SimpleSearchView
        ohos:id="$+id:searchView"
        ohos:height="match_parent"
        ohos:width="match_parent"
        app:backIconTint="#3F51B5"
        app:type="card"
        app:voiceSearch="true"
        tools:visibility="visible"/>
</StackLayout>


<TabList
    ohos:id="$+id:tab_list"
    ohos:height="50vp"
    ohos:width="match_parent"
    ohos:background_element="$color:colorPrimary"
    ohos:fixed_mode="true"
    ohos:selected_tab_indicator_color="#cc0066"
    ohos:selected_tab_indicator_height="3vp"
    ohos:text_alignment="vertical_center|horizontal_center"
    ohos:text_size="20fp"/>

<PageSlider
    ohos:id="$+id:container"
    ohos:height="match_parent"
    ohos:width="match_parent"/>

设置监听器

    simpleSearhView.setOnQueryTextListener(new SimpleSearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            Log.d("SimpleSearchView", "Submit:" + query);
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            Log.d("SimpleSearchView", "Text changed:" + newText);
            return false;
        }

        @Override
        public boolean onQueryTextCleared() {
            Log.d("SimpleSearchView", "Text cleared");
            return false;
        }
    });

设置TabLayout

simpleSearchView.setTabLayout(findViewById(R.id.tabLayout));

手动开启和关闭搜索框

simpleSearchView.showSearch();
simpleSearchView.closeSearch();

OnBackPressed

自动关闭搜索框
@Override
public void onBackPressed() {
    if (searchView.onBackPressed()) {
        return;
    }

    super.onBackPressed();
}

Style

Bar style (default):
app:type="bar"

Card style:
app:type="card"

打开和关闭监听

simpleSearchView.setOnSearchViewListener(new SimpleSearchView.SearchViewListener() {
    @Override
    public void onSearchViewShown() {
        Log.d("SimpleSearchView", "onSearchViewShown");
    }

    @Override
    public void onSearchViewClosed() {
        Log.d("SimpleSearchView", "onSearchViewClosed");
    }

    @Override
    public void onSearchViewShownAnimation() {
        Log.d("SimpleSearchView", "onSearchViewShownAnimation");
    }

    @Override
    public void onSearchViewClosedAnimation() {
        Log.d("SimpleSearchView", "onSearchViewClosedAnimation");
    }
});

属性:

     if (attrs.getAttr("type").isPresent()) {
            setCardStyle(AttrValue.get(attrs, "type", style));
        }
        if (attrs.getAttr("backIconAlpha").isPresent()) {
            setBackIconAlpha(AttrValue.get(attrs, "backIconAlpha", BACK_ICON_ALPHA_DEFAULT));
        }
        if (attrs.getAttr("iconsAlpha").isPresent()) {
            setIconsAlpha(AttrValue.get(attrs, "iconsAlpha", ICONS_ALPHA_DEFAULT));
        }
        if (attrs.getAttr("backIconTint").isPresent()) {
            setBackIconColor(AttrValue.get(attrs, "backIconTint", ResourceTable.Color_colorAccent));
        }
        if (attrs.getAttr("iconsTint").isPresent()) {
            setIconsColor(AttrValue.get(attrs, "iconsTint", ResourceTable.Color_colorAccent));
        }
        if (attrs.getAttr("cursorColor").isPresent()) {
            setCursorColor(AttrValue.get(attrs, "cursorColor", "#FF4081"));
        }
        if (attrs.getAttr("hintColor").isPresent()) {
            setHintTextColor(AttrValue.get(attrs, "hintColor", "#8A000000"));
        }
        if (attrs.getAttr("searchBackground").isPresent()) {
            ShapeElement shapeElement = new ShapeElement();
            String color = AttrValue.get(attrs, "searchBackground","#FFFFFF");
            shapeElement.setRgbColor(RgbColor.fromArgbInt(Color.getIntColor(color)));
            setSearchBackground(shapeElement);
        }
        if (attrs.getAttr("searchBackIcon").isPresent()) {
            setBackIconDrawable(AttrValue.get(attrs, "searchBackIcon",
                new VectorElement(getContext(), ResourceTable.Graphic_ic_arrow_back_black_24dp)));
        }
        if (attrs.getAttr("searchClearIcon").isPresent()) {
            setClearIconDrawable(AttrValue.get(attrs, "searchClearIcon",
                new VectorElement(getContext(), ResourceTable.Graphic_ic_close_black_24dp)));
        }
        if (attrs.getAttr("searchVoiceIcon").isPresent()) {
            setVoiceIconDrawable(AttrValue.get(attrs, "searchVoiceIcon",
                new VectorElement(getContext(), ResourceTable.Graphic_ic_voice_search_black_24dp)));
        }
        if (attrs.getAttr("voiceSearch").isPresent()) {
            enableVoiceSearch(AttrValue.get(attrs, "voiceSearch", isAllowVoiceSearch));
        }
        if (attrs.getAttr("voiceSearchPrompt").isPresent()) {
            setVoiceSearchPrompt(AttrValue.get(attrs, "voiceSearchPrompt", ""));
        }
        if (attrs.getAttr("hint").isPresent()) {
            setHint(AttrValue.get(attrs, "hint", "Search"));
        }
        if (attrs.getAttr("inputType").isPresent()) {
            setInputType(AttrValue.get(attrs, "inputType", TWO));
        }
        if (attrs.getAttr("textColor").isPresent()) {
            setTextColor(AttrValue.get(attrs, "textColor", ResourceTable.Color_default_textColor));
        }

测试信息

CodeCheck代码测试无异常

CloudTest代码测试无异常

病毒安全检测通过

当前版本demo功能与安卓原组件基本无差异

版本迭代

  • 1.0.0

版权和许可信息

Copyright (C) 2018 Fernando Augusto Heeren Falkiewicz

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.     

 

SimpleSearchView-master.zip 1.92M 30次下载
已于2021-10-13 21:03:25修改
收藏 1
回复
举报
回复
    相关推荐