如何获得使用 Google 地点自动完成功能的意图?

how do I get my intent to work with Google Place Autocomplete?

你能告诉我我需要做什么才能让我的意图生效吗?当用户单击 mEditInit 时,将出现位置下拉菜单。

当用户单击菜单中的位置时,我想将其发送回 mEditInit

我以前使用过意图,但在这种情况下不起作用。

public class MyActivity extends AppCompatActivity  {

  String stuff;

  private EditText mEditInit;

  public static final int AUTOCOMPLETE_REQUEST_CODE = 1;


  @Override
  protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_my);

    mEditInit = (EditText) findViewById(R.id.edit_message);

    mEditInit.setX(0);
    mEditInit.setY(250);

    //Get the bundle
    Bundle bundle = getIntent().getExtras();


    if (stuff != null && !stuff.isEmpty())
    //Extract the data…
    { stuff = bundle.getString("stuff");
      mEditInit.setText(stuff);
    }

    Places.initialize(this, getString(R.string.places_api_key));

    mEditInit.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Toast.makeText(MyActivity.this, "bowwow", Toast.LENGTH_SHORT).show();

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

      }
    });

  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
      if (resultCode == RESULT_OK) {

        Place place = Autocomplete.getPlaceFromIntent(data);
        //Toast.makeText(MyActivity.this, place.getName(), Toast.LENGTH_SHORT).show();

        Toast.makeText(MyActivity.this, place.getName(), Toast.LENGTH_SHORT).show();

        Intent i = new Intent(this, MyActivity.class);

        String getrec= place.getName();

        //Create the bundle
        Bundle bundle = new Bundle();

        //Add your data to bundle
        bundle.putString("stuff", getrec);

        //Add the bundle to the intent
        i.putExtras(bundle);

        // 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.
      }
      //  super.onActivityResult(requestCode, resultCode, data);
    }
  }

}

在您的 onActivityResult 方法中,您不需要任何额外的东西。

只需在 EditText:

上调用方法 setText()
@Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
      if (resultCode == RESULT_OK) {

        Place place = Autocomplete.getPlaceFromIntent(data);
        //Toast.makeText(MyActivity.this, place.getName(), Toast.LENGTH_SHORT).show();

        mEditInit.setText(place.getName());
      } 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.
      }
      //  super.onActivityResult(requestCode, resultCode, data);
    }
  }