我正在使用Option 2: Use an intent to launch the autocomplete activity如何在Android谷歌地方的自动完成活动中更改占位符文本?
是否可以将“搜索”占位符文本更改为诸如“进入您的城市”之类的内容。我发现a solution但是对于javascript,假设支持android。
它似乎可以用a PlaceAutocompleteFragment完成,但我使用自己的EditText通过使用意图启动自动完成,所以它没有帮助。
我正在使用Option 2: Use an intent to launch the autocomplete activity如何在Android谷歌地方的自动完成活动中更改占位符文本?
是否可以将“搜索”占位符文本更改为诸如“进入您的城市”之类的内容。我发现a solution但是对于javascript,假设支持android。
它似乎可以用a PlaceAutocompleteFragment完成,但我使用自己的EditText通过使用意图启动自动完成,所以它没有帮助。
这似乎是没有办法改变的占位符文本与自动完成活动或甚至与PlaceAutocompleteFragment,因为它也使用里面的自动完成活动。唯一的办法是使用AutoCompleteTextView,你可以完全控制它。 Google提供了完整的示例代码,这些代码也易于集成并提供良好的用户体验。
https://github.com/googlesamples/android-play-places/tree/master/PlaceCompleteAdapter
我创建了一个功能请求here,希望这将在未来得到支持。
这是你在寻找什么?
SearchView searchView = (SearchView)
menu.findItem(R.id.menu_search).getActionView();
searchView.setQueryHint("Your text");
菜单是什么? – thanhbinh84
不幸的是,没有办法用自动完成意图改变提示文本。 您需要制作自己的小部件,例如PlaceAutocompleteFragment。
不,没有办法,如果你想比你自己创造。 但是随着你使用EditText(在注释部分提及),所以可能这种技术对你很有用。
代码:
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"
/>
,并为我所用android:background="@drawable/search_background"
所以ü必须创建自己的背景形状(符合你的需要)。
对不起,我没有时间检查那个答案,但我的意思是如何改变第二张图片中的'搜索'文本。 – thanhbinh84
您可以通过两种方式实现谷歌的地方的功能: 1)通过使用PlaceAutocomplete.IntentBuilder 2。)通过使用PlaceAutocompleteFragment
案例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.
}
}
案例2: 一)创建一个名为 “AddressSelectActivity.java” b新的活动)不要忘记定义。 manifest.xml中的活动
以下是以下代码:
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);
}
});
}
}
'.zzih(“提示”)',无法解析方法。 –
如果您能够找到build()方法,请搜索该类中的方法,以便实现相同。 转到该课程并手动搜索。 – Maddy
是有可能,您使用的AutoCompleteTextView为此做这样的
EditText et = (EditText)findViewById(R.id.place_autocomplete_search_input);
et.setHint("enter your city");
? –
不,我使用正常的EditText视图,当用户点击它时,它会启动自动完成活动。 Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY) .setFilter(typeFilter) .build(this); startActivityForResult(intent,PLACE_AUTOCOMPLETE_REQUEST_CODE); – thanhbinh84
您可以使用AutoCompleteTextView。如果您使用此功能,您也不必跳转到其他活动。 –