我有AutocompleteSupportFragment API的Places:

<fragment android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/auto_complete"
        android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
        android:outlineProvider="bounds"
        android:elevation="5dp"
        app:backgroundTint="@color/colorWhite"
        android:imeOptions="actionSearch"/>

我已经初始化并正确设置了所有内容,但我不知道如何在键盘的Search键上设置触发器。我试过了,但是关键是完全忽略了代码:
autocompleteSupportFragment.getView().setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                if ((keyEvent.getAction() == KeyEvent.KEYCODE_SEARCH || keyEvent.getAction() == KeyEvent.KEYCODE_ENTER || i == EditorInfo.IME_ACTION_SEARCH || i == EditorInfo.IME_ACTION_DONE || keyEvent.getAction() == KeyEvent.ACTION_DOWN || keyEvent.getAction() == KeyEvent.KEYCODE_ENTER) && keyEvent.isTracking() && !keyEvent.isCanceled()) {
                    Toast.makeText(getApplicationContext(), "Could not predict", Toast.LENGTH_SHORT).show();
                    return true;
                }
                return false;
            }
        });

有没有我尝试过的东西?

最佳答案

“聚焦模式” =我们实际上可以在其中键入地址的位置。

当我们使用嵌入式片段时,这意味着我们使用默认的占位符来触发“聚焦模式”。如果要从自定义占位符触发“聚焦模式”,则需要从布局中排除片段。

使用嵌入式片段/默认占位符

//GRADLE
implementation 'com.google.android.libraries.places:places:2.0.0'
remove the implementation 'com.google.android.gms:play-services-places:x.y'

//XML LAYOUT
<fragment
    android:id="@+id/autocomplete_fragment"
    android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

// ACTIVITY
onCreate {
    var autocompleteFragment: AutocompleteSupportFragment? = null
    if (!Places.isInitialized()) {
        Places.initialize(applicationContext, "API KEY");
    }

    // Bind the fragment in layout xml
    autocompleteFragment =
        supportFragmentManager.findFragmentById(R.id.autocomplete_fragment) as? AutocompleteSupportFragment

    // Specify the types of place data to return.
    autocompleteFragment?.setPlaceFields(Arrays.asList(Place.Field.LAT_LNG))

    // Set up a PlaceSelectionListener to handle the response.
    autocompleteFragment?.setOnPlaceSelectedListener(object : PlaceSelectionListener {
        override fun onPlaceSelected(place: Place) {
            val str = "lat: ${place.latLng?.latitude}, long: ${place.latLng?.longitude}"
            Toast.makeText(mAct, str, Toast.LENGTH_SHORT).show()

        }

        override fun onError(status: Status) {
            Toast.makeText(mAct, "ERROR", Toast.LENGTH_SHORT).show()
        }

    })
}

b。使用自定义占位符
//GRADLE
implementation 'com.google.android.libraries.places:places:2.0.0'
remove the implementation 'com.google.android.gms:play-services-places:x.y'

// XML LAYOUT
    <Button
        android:layout_width="wrap_content"
        android:id="@+id/home_BTN_autocomplete"
        android:text="show autocomplete widget"
        android:layout_height="wrap_content" />

// ACTIVITY
val AUTOCOMPLETE_REQUEST_CODE = 1212
var mAct: Context? = null
onCreate {
    mAct = this // Needed for onactivityresult hook
    if (!Places.isInitialized()) {
        Places.initialize(applicationContext, "API-KEY");
    }
    home_BTN_autocomplete.setOnClickListener {
        val fields = Arrays.asList(Place.Field.LAT_LNG)
        val intent = Autocomplete.IntentBuilder(AutocompleteActivityMode.FULLSCREEN, fields).build(this)
        startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE)
    }
}

 onActivityResult {
    if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
        if (resultCode == Activity.RESULT_OK && data != null) {
            val place = Autocomplete.getPlaceFromIntent(data)
            val str = "lat: ${place.latLng?.latitude}, long: ${place.latLng?.longitude}"
            Toast.makeText(mAct, str, Toast.LENGTH_SHORT).show()

        } else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
        } else if (resultCode == Activity.RESULT_CANCELED) {
        }
    }
}

08-18 13:26