在操作栏搜索中集成Google Place AutoCompleteAPI?

时间:2016-04-29 10:08:45

标签: java android google-maps google-places-api

我已在Google Place Autocomplete API成功整合Activity。新流程应该是这样的:

  • 用户点击Menu search图标。
  • Google place autocomplete的搜索框应与其对齐。
  • 无论用户选择autocomplete。它应该更新Menu search Icon

以下是代码:

MENU_ITEM

...
<item android:id="@+id/search"
      android:title="@string/action_search"
      android:icon="@drawable/ic_search"
      android:orderInCategory="100"
      app:showAsAction="always" />
...

MainActivity

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_add_location, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.search) {
        // Method #3
        try {
            Intent intent = new PlaceAutocomplete.IntentBuilder
                    (PlaceAutocomplete.MODE_FULLSCREEN)
                    .setBoundsBias(BOUNDS_MOUNTAIN_VIEW)
                    .build(AddLocationActivityNew.this);
            startActivityForResult(intent, REQUEST_SELECT_PLACE);
        } catch (GooglePlayServicesRepairableException |
                GooglePlayServicesNotAvailableException e) {
            e.printStackTrace();
        }
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onPlaceSelected(Place place) {
    Log.i(LOG_TAG, "Place Selected: " + place.getName());
    txt_location.setText(getString(R.string.formatted_place_data, place
            .getName(), place.getAddress(), place.getPhoneNumber(), place
            .getWebsiteUri(), place.getRating(), place.getId()));
    if (!TextUtils.isEmpty(place.getAttributions())){
        txt_attributions.setText(Html.fromHtml(place.getAttributions().toString()));
    }
}

现在,我点击了菜单项图标。它正在开展一项新活动: enter image description here

如何将此活动整合到操作栏中?

如果我将MODE从FullScreen更改为Overlays。它是这样的:

enter image description here

如何在Android操作栏中集成整个搜索栏?

1 个答案:

答案 0 :(得分:0)

在菜单文件中定义搜索按钮时,请尝试使用app:actionViewClass="android.support.v7.widget.SearchView"