2017-07-28 17 views
2

我正在使用Option 2: Use an intent to launch the autocomplete activity如何在Android谷歌地方的自動完成活動中更改佔位符文本?

是否可以將「搜索」佔位符文本更改爲諸如「進入您的城市」之類的內容。我發現a solution但是對於javascript,假設支持android。

它似乎可以用a PlaceAutocompleteFragment完成,但我使用自己的EditText通過使用意圖啓動自動完成,所以它沒有幫助。

enter image description here

+0

? –

+0

不,我使用正常的EditText視圖,當用戶點擊它時,它會啓動自動完成活動。 Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY) .setFilter(typeFilter) .build(this); startActivityForResult(intent,PLACE_AUTOCOMPLETE_REQUEST_CODE); – thanhbinh84

+0

您可以使用AutoCompleteTextView。如果您使用此功能,您也不必跳轉到其他活動。 –

回答

1

這似乎是沒有辦法改變的佔位符文本與自動完成活動或甚至與PlaceAutocompleteFragment,因爲它也使用裏面的自動完成活動。唯一的辦法是使用AutoCompleteTextView,你可以完全控制它。 Google提供了完整的示例代碼,這些代碼也易於集成並提供良好的用戶體驗。

https://github.com/googlesamples/android-play-places/tree/master/PlaceCompleteAdapter

我創建了一個功能請求here,希望這將在未來得到支持。

0

這是你在尋找什麼?

SearchView searchView = (SearchView) 
menu.findItem(R.id.menu_search).getActionView(); 
searchView.setQueryHint("Your text"); 
+0

菜單是什麼? – thanhbinh84

2

不幸的是,沒有辦法用自動完成意圖改變提示文本。 您需要製作自己的小部件,例如PlaceAutocompleteFragment。

4

不,沒有辦法,如果你想比你自己創造。 但是隨着你使用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"所以ü必須創建自己的背景形狀(符合你的需要)。

第一張圖片: enter image description here

第二張圖: enter image description here

THRID圖片: enter image description here

四圖片: enter image description here

+0

對不起,我沒有時間檢查那個答案,但我的意思是如何改變第二張圖片中的'搜索'文本。 – thanhbinh84

0

您可以通過兩種方式實現谷歌的地方的功能: 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); 
      } 
     }); 
    } 
} 
+0

'.zzih(「提示」)',無法解析方法。 –

+0

如果您能夠找到build()方法,請搜索該類中的方法,以便實現相同。 轉到該課程並手動搜索。 – Maddy

0

是有可能,您使用的AutoCompleteTextView爲此做這樣的

EditText et = (EditText)findViewById(R.id.place_autocomplete_search_input); 
    et.setHint("enter your city");