首页 > 解决方案 > Google PlaceAutoCompleteFragment 在被点击时失去焦点

问题描述

我的 PlaceAutoComplete 搜索栏在被点击时失去焦点。有时可以点击并搜索,但它没有找到任何地方,然后再次失去焦点。

我已经尝试遵循此处提供的解决方案:Android Google Places API, getAutocompletePredictions 返回状态 'PLACES_API_ACCESS_NOT_CONFIGURED'

我有一个谷歌地图片段和谷歌地方片段的活动:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MapsActivity"
    android:orientation="vertical"
    android:weightSum="1">

    <fragment
        android:id="@+id/place_autocomplete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
        />

    <fragment
        android:id="@+id/map"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

我在我的谷歌开发者控制台中启用了谷歌地方:

在此处输入图像描述

当然,我正在使用 API 密钥,并且由于 Google 地图功能正常工作,它已正确完成:

  <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

会不会是依赖问题?我的相关依赖项:

implementation 'com.google.android.gms:play-services-maps:17.0.0'
implementation 'com.google.android.gms:play-services-location:17.0.0'
implementation 'com.google.android.gms:play-services-places:17.0.0'
implementation 'com.google.android.libraries.places:places-compat:2.2.0'

我添加了 places-compat (最后一个依赖项)只是因为有人建议它,但它没有帮助。

什么可能导致这个问题?谢谢

标签: androidandroid-studiogoogle-location-services

解决方案


// In Your Activity / Fragment While Clicking SearchView Call This (Kotlin)

private var mAutoCompleteRequestCode: Int = 1

    if (!Places.isInitialized()) {
                    Places.initialize(this.context!!, getString(R.string.google_maps_key), Locale.US)
                } else {
                    val mFields = listOf(
                        Place.Field.ID,
                        Place.Field.NAME,
                        Place.Field.LAT_LNG,
                        Place.Field.ADDRESS,
                        Place.Field.ADDRESS_COMPONENTS
                    )
                    val intent =
                        Autocomplete.IntentBuilder(AutocompleteActivityMode.FULLSCREEN, mFields).build(
                            this.context!!
                        )
                    startActivityForResult(intent, mAutoCompleteRequestCode)
                }



override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        Log.d(
            mTAG,
            "" + requestCode + "\t" + resultCode + "\t" + data
        )

        if (requestCode == mAutoCompleteRequestCode) {
            if (resultCode == RESULT_OK) {
                val place = Autocomplete.getPlaceFromIntent(data!!)

                val mLatitude = place.latLng?.latitude.toString()
                val mLongitude = place.latLng?.longitude.toString()
                val mAddress = place.name

            } else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
                val status = Autocomplete.getStatusFromIntent(data!!)
                Constant.logE(mTAG, "Auto Complete Places Search Error : ", status.statusMessage!!)
            }
        } else {

        }
    }

推荐阅读