【问题标题】:How to remove search icon from PlaceAutocompleteFragment in Android如何从 Android 中的 PlaceAutocompleteFragment 中删除搜索图标
【发布时间】:2017-02-13 18:45:11
【问题描述】:

我正在使用 PlaceAutocompleteFragment 搜索地点。 下面是我的xml代码

<fragment
android:id="@+id/place_source"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@color/colorPrimary"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment" />

我想删除默认带有片段的搜索图标。

【问题讨论】:

    标签: android google-places-api


    【解决方案1】:

    您可以使用这一行来获取搜索图标的引用:((View)findViewById(R.id.place_autocomplete_search_button)).setVisibility(View.GONE);

    这一行得到EditText的引用:((EditText)findViewById(R.id.place_autocomplete_search_input))

    【讨论】:

    • 这只适用于一个 PlaceAutocompleteFragment 。我有两个 PlaceAutocompleteFragment 并且它设置为第一个 PlaceAutocompleteFragment
    【解决方案2】:

    如果我们有 2 个 autocompleteFragment,我们可以像这样隐藏图标:

    autocompleteFragment_from.getView().findViewById(R.id.place_autocomplete_search_button).setVisibility(View.GONE);
    autocompleteFragment_to.getView().findViewById(R.id.place_autocomplete_search_button).setVisibility(View.GONE);
    

    【讨论】:

      【解决方案3】:

      上述解决方案对我不起作用,因为我使用的是 2.1.0

      这个版本的ID是:

      搜索图标:places_autocomplete_search_button

      清除图标:places_autocomplete_clear_button

      编辑文本:places_autocomplete_search_input

      所以为了简单地使用它们,

      autocompleteSupportFragment.getView().findViewById(R.id.places_autocomplete_search_button).setVisibility(View.GONE);
      

      【讨论】:

      • R 来自哪个包?
      • 哦,原来是:com.google.android.libraries.places.R 但是,search_input 的结果似乎与最终地址不同。
      • (places_autocomplete_clear_button) 来自包:com.google.android.libraries.places.R.id.places_autocomplete_clear_button
      【解决方案4】:

      这个问题有点老了,但我会留在这里给未来的观众。

      虽然@rajat44 的回答在您有一个 PlaceAutocompleteFragment 时解决了这个问题,但如果您有多个,您可以这样做:

      pacf.getView().findViewById(R.id.place_autocomplete_search_button).setVisibility(View.GONE);
      pacf2.getView().findViewById(R.id.place_autocomplete_search_button).setVisibility(View.GONE);
      

      【讨论】:

        【解决方案5】:

        很遗憾,无法自定义 PlaceAutocompleteFragment 的外观,但由于它所做的只是启动 PlaceAutocomplete 活动以响应点击,因此很容易通过适用于您的应用的 UI 复制其功能。

        1. 创建自定义片段 (instructions)。
        2. 将您想要的 UI 元素添加到片段中。
        3. 将 OnClickListener 添加到启动自动完成活动的 UI 元素 (as described in the API docs)。

        【讨论】:

        • 实际上我能够获得用作 PlaceAutocompleteFragment 一部分的 EditText 视图的引用,例如 src = ((EditText)placePickup.getView().findViewById(R.id.place_autocomplete_search_input)); target = ((EditText)placeDrop.getView().findViewById(R.id.place_autocomplete_search_input));
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多