在 editText onClick 中膨胀 AutoCompleteSupportFragement

Inflating an AutoCompleteSupportFragement in an editText onClick

我正在尝试在 editText 的 onClick 事件中膨胀使用 Google Places API 的 AutoCompleteSupportFragment。

我想在单击 editText 时膨胀该片段,以便用户可以输入一个位置,select 根据自动完成片段中的建议,位置名称将显示在 editText 上。

我检查了这个 link here,但它对我不起作用。

places_auto_complete_fragment.xml

    <?xml version="1.0" encoding="utf-8"?>
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/place_autocomplete_fragment"
    android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment" />

我是怎么给它充气的

sourceEditText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            LayoutInflater layoutInflater = getLayoutInflater();
            layoutInflater.inflate(R.layout.places_auto_complete_fragment,addTripLinearLayout,false);
        }
    });

感谢任何帮助。谢谢

  1. AutocompleteSupportFragment 向用户显示一个搜索框按钮,点击后显示一个搜索框UI
  2. 展开布局会加载 UI,但您必须将其添加到视图中才能使其可见。

在 editText 中使用 Intent 构建器代替片段....

在 Oncreate 上初始化

   // Initialize Places.
   Places.initialize(getApplicationContext(), "***YOUR API KEY***");

   // Create a new Places client instance.
   PlacesClient placesClient = Places.createClient(this);

然后在点击事件的编辑文本上使用下面的代码...

List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME);
// Start the autocomplete intent.
Intent intent = new Autocomplete.IntentBuilder(AutocompleteActivityMode.FULLSCREEN, fields).build(this);
startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            Place place = Autocomplete.getPlaceFromIntent(data);
            Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
        } else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
            // TODO: Handle the error.
            Status status = Autocomplete.getStatusFromIntent(data);
            Log.i(TAG, status.getStatusMessage());
        } else if (resultCode == RESULT_CANCELED) {
            // The user canceled the operation.
        }
    }
}

请参考以下link ...

if you are using only fragment please refer below code

       AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
                getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);
    autocompleteFragment.setCountry("IN");    //country type
    autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME)); 
   //to indicate the types of place data that you want to get.