问题
I am using Option 2: Use an intent to launch the autocomplete activity
Is it possible to change the "search" place holder text to something such as "enter your city". I found a solution but for javascript, suppose support android as well.
It seems it could be done with a PlaceAutocompleteFragment, but I am using my own EditText to launch autocomplete by using an intent, so it is not helpful.
回答1:
No there is no way and if you want than you have to create your own. But as u use the EditText(mention in Comment Section) so may be this technique is useful for your.
Code:
PlaceAutocompleteFragment autocompleteFragment;
autocompleteFragment = (PlaceAutocompleteFragment)
getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
autocompleteFragment.setOnPlaceSelectedListener(this);
autocompleteFragment.setHint("Search New Location");
XMl :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/left_drawable_padding"
android:layout_marginTop="@dimen/margin_five"
android:layout_marginLeft="@dimen/margin_five"
android:layout_marginRight="@dimen/margin_five"
android:background="@drawable/search_background"
android:orientation="vertical">
<fragment
android:id="@+id/place_autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
/>
and as i used android:background="@drawable/search_background" so u have to create your own background shape(acc. to your need).
first picture :
Second picture :
thrid picture :
Fourth picture :
回答2:
Unfortunately, there is no way to change the hint text with the autocomplete intent. You need to make your own widget, like the PlaceAutocompleteFragment.
回答3:
It seems there is no way to change placeholder text with either auto complete activity or even with PlaceAutocompleteFragment because it also use autocomplete activity inside. The only way is using AutoCompleteTextView that you have full control on it. Google provides full sample code that is also easy to integrate and provide good UX.
https://github.com/googlesamples/android-play-places/tree/master/PlaceCompleteAdapter
I create a feature request here, hopefully it will be supported in the future.
回答4:
Is this what you looking for?
SearchView searchView = (SearchView)
menu.findItem(R.id.menu_search).getActionView();
searchView.setQueryHint("Your text");
回答5:
You can achieve google places functionality by two ways : 1.) By using PlaceAutocomplete.IntentBuilder 2.) By using PlaceAutocompleteFragment
CASE 1 :
private void openPlacesSearch()
{
private static final int PLACE_AUTOCOMPLETE_REQUEST_CODE = 1001;
try {
Intent intent =
new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN)
.zzih("Enter pickup location") //set the hint text here
.build(this);
startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE);
}
catch (GooglePlayServicesRepairableException e) {
// TODO: Handle the error.
} catch (GooglePlayServicesNotAvailableException e) {
// TODO: Handle the error.
}
}
CASE 2: a.) Create a new Activity named "AddressSelectActivity.java" b.) Don't forget to define that activity in manifest.xml
Here is the code below :
public class AddressSelectActivity extends Activity {
private static final String TAG = AddressSelectActivity.class.getSimpleName();
private static final int PLACE_AUTOCOMPLETE_REQUEST_CODE = 1001;
private PlaceAutocompleteFragment autocompleteFragment;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_address_select);
addAddressSuggestionListener();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode)
{
case PLACE_AUTOCOMPLETE_REQUEST_CODE:
if (resultCode == RESULT_OK) {
Place place = PlaceAutocomplete.getPlace(this, data);
Log.i(TAG, "Place: " + place.getName());
LatLng latLng = place.getLatLng();
} else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
Status status = PlaceAutocomplete.getStatus(this, data);
// TODO: Handle the error.
Log.i(TAG, status.getStatusMessage());
} else if (resultCode == RESULT_CANCELED) {
// The user canceled the operation.
}
break;
default:
break;
}
}
private void addAddressSuggestionListener()
{
autocompleteFragment = (PlaceAutocompleteFragment)
getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
autocompleteFragment.setHint("Search Your Location...");
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
// TODO: Get info about the selected place.
Log.i(TAG, "Place: " + place.getName());
}
@Override
public void onError(Status status) {
// TODO: Handle the error.
Log.i(TAG, "An error occurred: " + status);
}
});
}
}
回答6:
If you are using an auto complete text view, use setHint on the layout control, and the top-left title and hint text will both update. If you just update the contained edit view, the top-left title will not change.
回答7:
Yes it is possible, do it like this
EditText et = (EditText)findViewById(R.id.place_autocomplete_search_input);
et.setHint("enter your city");
来源:https://stackoverflow.com/questions/45366037/how-to-change-placeholder-text-in-an-autocomplete-activity-of-android-google-pla