Android Google Places API - PlaceAutocompleteFragment清除按钮监听器

Bha*_*esh 6 android android-location android-fragments google-places google-maps-android-api-2

我在我的项目中使用Google Places API -我的项目中的PlaceAutocompleteFragment API来搜索位置,当用户选择位置然后使用位置lat-long获取记录并将其显示到ListView.

现在问题是每当用户点击PlaceAutocompleteFragment的清除按钮(X)时,我想要清除ListView项目,我需要听清楚按钮onClick吗?怎么做?

在此输入图像描述
任何帮助将不胜感激.

autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
    @Override
    public void onPlaceSelected(Place place) {
        LatLng latLng=place.getLatLng();
        //hit web service, response parsing and add it to listview.
    }

    @Override
    public void onError(Status status) {
        Log.i(TAG, "An error occurred: " + status);
    }
});
Run Code Online (Sandbox Code Playgroud)

Bha*_*esh 22

PlaceAutocompleteFragment查看我找到的那些视图ID 的源代码并设置onClickListener如下:

autocompleteFragment.getView().findViewById(R.id.place_autocomplete_clear_button)
    .setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // example : way to access view from PlaceAutoCompleteFragment
            // ((EditText) autocompleteFragment.getView()
            // .findViewById(R.id.place_autocomplete_search_input)).setText(""); 
            autocompleteFragment.setText("");
            view.setVisibility(View.GONE);
        }
});
Run Code Online (Sandbox Code Playgroud)

  • 更改 place_autocomplete_clear_button 而不是 place_autocomplete_clear_button。对我来说“s”是问题所在 (2认同)