android - 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 (最后一个依赖项)只是因为有人建议它,但它没有帮助。
什么可能导致这个问题?谢谢
解决方案
// 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 {
}
}
推荐阅读
- linux - 如何使用 sed 或 awk 处理指定列的内容?
- python-3.x - 使用 Matplotlib 创建一个简单的时间表
- java - Firebase phone auth 替换了 userimage 和 Displayname
- php - 如何在 Laravel 中使用唯一的“电子邮件”字段在 Controller 中进行更新?
- html - 表格单元格字体很奇怪而且比它应该的大(粗体?)
- php - Laravel 5.8 错误 500 上传到共享主机 cpanel
- python - Discord.py bot 使用日期时间返回此(类型错误:不支持的操作数类型 -: 'datetime.datetime' 和 'int')
- javascript - 如何使用 mongoose 从 mongoDB 中获取一定数量的对象?(Node.js)
- bash - 为什么 /usr/local/bin/python3 的链接与“which python3”的路径不同?
- azure-cosmosdb - Azure Cosmos Gremlin out() 奇怪的行为