【问题标题】:Google PlaceAutoCompleteFragment loses focus when being clickedGoogle PlaceAutoCompleteFragment 在被点击时失去焦点
【发布时间】:2020-02-12 07:35:20
【问题描述】:

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

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

我有一个包含 Google 地图片段和 Google 地方片段的活动:

<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>

我在我的 google 开发者控制台中启用了 Google Places:

当然,我正在使用 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(最后一个依赖项)只是因为有人建议它,但它没有帮助。

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

【问题讨论】:

    标签: android android-studio google-location-services


    【解决方案1】:
    // 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 {
    
            }
        }
    

    【讨论】:

    • 这个 Places 类应该从哪里导入?我从 location.places 或 places.compat 尝试过,但 Places.isInitialized 未解决,而且 Place.Field 也未解决。谢谢
    • 添加这个实现 'com.google.android.libraries.places:places:2.2.0'
    • 哦,好的,谢谢,我仍然在每个 Place.Field(列表中)中得到未解决的引用,您知道可能是什么问题吗?
    • 你正在使用 Kotlin / java
    • 我正在使用 Kotlin
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-21
    • 2012-04-30
    • 2011-10-13
    • 1970-01-01
    • 2018-08-16
    • 1970-01-01
    相关资源
    最近更新 更多