
废话不多说,先看效果,如果大家感觉不错,请参考实现代码
这个功能我是用Fragmentdialog里面做的,也遇到不少坑
第一,就是设置背景的drawable为纯白色导致键盘d出的时候,recyclervIEw的布局被顶上去导致出现白色布局,有点扎眼;最后改成了设置为和背景色一个颜色就和好了
Window window = getDialog().getwindow(); WindowManager.LayoutParams lp = window.getAttributes(); lp.gravity = Gravity.CENTER; window.setBackgroundDrawable(new colorDrawable(ContextCompat.getcolor(getActivity(),R.color.color_gray_f2))); window.setAttributes(lp);
布局
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" xmlns:tools="http://schemas.androID.com/tools" androID:background="@color/color_gray_f2" androID:orIEntation="vertical"> <relativeLayout androID:ID="@+ID/search_maps_bar" androID:layout_wIDth="match_parent" androID:layout_height="50dp" androID:layout_centerHorizontal="true" androID:layout_marginleft="15dp" androID:layout_marginRight="15dp" androID:layout_margintop="10dp" androID:background="@drawable/new_card"> <Imagebutton androID:ID="@+ID/dialog_search_back" androID:layout_wIDth="50dp" androID:layout_height="match_parent" androID:layout_centerVertical="true" androID:layout_margin="2dp" androID:background="@drawable/button_background_selector" androID:src="@drawable/ic_qu_appbar_back"/> <Imagebutton androID:ID="@+ID/dialog_serach_btn_search" androID:layout_wIDth="50dp" androID:layout_height="match_parent" androID:layout_alignParentRight="true" androID:layout_centerVertical="true" androID:layout_margin="2dp" androID:background="@drawable/button_background_selector" androID:src="@drawable/ic_qu_search" tools:ignore="ContentDescription,RtlHardcoded"/> <EditText androID:ID="@+ID/dialog_search_et" androID:layout_wIDth="wrap_content" androID:layout_height="match_parent" androID:layout_centerInParent="true" androID:layout_marginleft="5.0dip" androID:layout_marginRight="5.0dip" androID:layout_toleftOf="@+ID/dialog_serach_btn_search" androID:layout_toRightOf="@+ID/dialog_search_back" androID:background="@androID:color/transparent" androID:completionThreshold="1" androID:dropDownVerticalOffset="1.0dip" androID:hint="请输入关键字" androID:imeOptions="actionSearch|flagNoExtractUi" androID:inputType="text|textautoComplete" androID:maxHeight="50dp" androID:maxLength="20" androID:minHeight="50dp" androID:singleline="true" androID:textcolor="#000000" androID:textSize="16.0sp"/> </relativeLayout> <androID.support.v7.Widget.RecyclerVIEw androID:ID="@+ID/dialog_search_recyclervIEw" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:layout_marginleft="15dp" androID:layout_marginRight="15dp" androID:layout_margintop="@dimen/dp_10" /></linearLayout>
第二个问题是键盘d出的时候,会出现dialog布局整体被顶上去
最后通过设置 style来解决
@OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //解决dialogfragment布局不被顶上去的方法 setStyle(DialogFragment.STYLE_norMAL,androID.R.style.theme_Black_NoTitlebar); }最后就是实现搜索功能了
第一个点击搜索时,键盘和搜索按钮两个都是同样的效果
/** * 搜索功能 */ private voID searchLocationPoi() { //关闭键盘 KeyBoardUtils.closeKeybord(poiSearchInMaps,BaseApplication.mContext); if (TextUtils.isEmpty(poiSearchInMaps.getText().toString().trim())) { ToastUtils.showToastCenter("内容为空!"); } else { query = new PoiSearch.query(poiSearchInMaps.getText().toString().trim(),"","");// 第一个参数表示搜索字符串,第二个参数表示poi搜索类型,第三个参数表示poi搜索区域(空字符串代表全国) query.setPageSize(20);// 设置每页最多返回多少条poiitem query.setPageNum(0);// 设置查第一页 poiSearch = new PoiSearch(getActivity(),query); poiSearch.setonPoiSearchListener(this); poiSearch.searchPOIAsyn(); } }然后回调中进行处理
@OverrIDe public voID onPoiSearched(PoiResult poiResult,int errcode) { Logger.e(poiResult.getPois().toString() + "" + errcode); if (errcode == 1000) { datas = new ArrayList<>(); ArrayList<PoiItem> pois = poiResult.getPois(); for (int i = 0; i < pois.size(); i++) { LocationBean locationBean = new LocationBean(); locationBean.Title = pois.get(i).getTitle(); locationBean.snippet = pois.get(i).getSnippet(); datas.add(locationBean); } searchCaradapter.setNewData(datas); } }还有就是监听EditText里面内容的变化来搜索,其实也很简单
poiSearchInMaps.addTextChangedListener(new TextWatcher() { @OverrIDe public voID beforeTextChanged(CharSequence charSequence,int i,int i1,int i2) { } @OverrIDe public voID onTextChanged(CharSequence charSequence,int i2) { textChangeSearch(charSequence); } @OverrIDe public voID afterTextChanged(Editable editable) { } }); /** * 监听edittext内容的变化,去搜索 */ private voID textChangeSearch(CharSequence charSequence) { String content = charSequence.toString().trim();//获取自动提示输入框的内容 Logger.e(content); inputtipsquery inputtipsquery = new inputtipsquery(content,"");//初始化一个输入提示搜索对象,并传入参数 inputtips inputtips = new inputtips(getActivity(),inputtipsquery);//定义一个输入提示对象,传入当前上下文和搜索对象 inputtips.setinputtipsListener(new inputtips.inputtipsListener() { @OverrIDe public voID onGetinputtips(List<Tip> List,int errcode) { Logger.e(List.toString() + errcode); if (errcode == 1000 && List != null) { datas = new ArrayList<>(); for (int i = 0; i < List.size(); i++) { LocationBean locationBean = new LocationBean(); Tip tip = List.get(i); locationBean.latitude = tip.getPoint().getLatitude(); locationBean.longitude = tip.getPoint().getLongitude(); locationBean.snippet = tip.getname(); locationBean.Title = tip.getdistrict(); datas.add(locationBean); } searchCaradapter.setNewData(datas); } } });//设置输入提示查询的监听,实现输入提示的监听方法onGetinputtips() inputtips.requestinputtipsAsyn();//输入查询提示的异步接口实现 }ok,搞定,最后只需要搞个回调,把Search后点击的item传回去就好了.希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!
以上是内存溢出为你收集整理的Android 高德地图之poi搜索功能的实现代码全部内容,希望文章能够帮你解决Android 高德地图之poi搜索功能的实现代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)