gpt4 book ai didi

java - 如何获得使用 Google 地点自动完成功能的 Intent ?

转载 作者:行者123 更新时间:2023-12-02 09:10:59 26 4
gpt4 key购买 nike

你能告诉我我需要做什么才能让我的 Intent 发挥作用吗?当用户单击mEditInit时,将出现“位置”下拉菜单。

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

我之前使用过 Intent ,但在这种情况下不起作用。

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);
}
}

关于java - 如何获得使用 Google 地点自动完成功能的 Intent ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59418365/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com