溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android中怎么實現poi搜索功能

發(fā)布時間:2021-06-29 15:06:10 來源:億速云 閱讀:159 作者:Leah 欄目:移動開發(fā)

Android中怎么實現poi搜索功能,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

第一,就是設置背景的drawable為純白色導致鍵盤彈出的時候,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>

第二個問題是鍵盤彈出的時候,會出現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搜索區(qū)域(空字符串代表全國)
      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);
    }
  }

    還有就是監(jiān)聽EditText里面內容的變化來搜索,其實也很簡單

 poiSearchInMaps.addTextChangedListener(new TextWatcher() {
      @Override
      public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
      }
      @Override
      public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        textChangeSearch(charSequence);
      }
      @Override
      public void afterTextChanged(Editable editable) {
      }
    });
  /**
   * 監(jiān)聽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);
        }
      }
    });//設置輸入提示查詢的監(jiān)聽,實現輸入提示的監(jiān)聽方法onGetInputtips()
    inputtips.requestInputtipsAsyn();//輸入查詢提示的異步接口實現
  }

關于Android中怎么實現poi搜索功能問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業(yè)資訊頻道了解更多相關知識。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI