Android Place自动完成片段:无法设置文本

时间:2019-02-03 02:06:46

标签: android google-places-api placeautocompletefragment

Google最近更新了其Android版Places SDK,因此现在我也正在更新代码。我正在尝试使用AutocompleteSupportFragment来允许用户设置其地址。

这是我的代码:

mAddressEditText = (AutocompleteSupportFragment) getSupportFragmentManager().findFragmentById(R.id.address);
mAddressEditText.setPlaceFields(Arrays.asList(Place.Field.ADDRESS, Place.Field.LAT_LNG));
mAddressEditText.setHint("Address");
mAddressEditText.setText("Test1");                      // Works fine at the beginning, disappears after selecting a place and shows only the hint
mAddressEditText.setOnPlaceSelectedListener(new PlaceSelectionListener() {
    @Override
    public void onPlaceSelected(Place place) {
        Log.d(TAG, "Place Selected");
        // Other Stuff
        mAddressEditText.setText("Test2");              // Doesn't Work, all I can see is the hint
        mAddressEditText.setText(place.getAddress());   // Doesn't Work, all I can see is the hint
    }

    @Override
    public void onError(Status status) {
        Log.e(TAG, "An error occurred: " + status);
        invalidAddressDialog.show();
    }
});

在以前的SDK中,该片段会自动将文本设置为所选地址。这在新的SDK中不起作用(不确定是否是故意的)。 因此,我尝试改为手动进行设置。如您在我的代码注释中所看到的,在听众之外使用setText可以正常工作。在听众内部,他们没有。

我做错了还是这是一个错误?

编辑: 这么长的时间,我仍然无法对此进行适当的修复。 要非常清楚,我可以从片段,不工作setText

的唯一的事情得到正确的地址

然而,由于一些答案声明他们没有得到同样的问题,我就开始想它可能我使用涉及到图书馆的版本?

这些是我在build.gradle中拥有的库:

api 'com.android.support:appcompat-v7:28.0.0'
api 'com.android.support:support-annotations:28.0.0'

api 'com.android.support:multidex:1.0.3'

api 'com.google.firebase:firebase-core:16.0.8'
api 'com.google.firebase:firebase-auth:16.2.1'
api 'com.google.firebase:firebase-firestore:18.2.0'
api 'com.google.firebase:firebase-storage:16.1.0'
api 'com.google.android.libraries.places:places:1.1.0'

9 个答案:

答案 0 :(得分:2)

setText一直给我同样的问题-我认为这一定是一个错误。但是,我发现了有关提示的一些解决方法。在您的onPlaceSelected中,您可以放置​​以下内容:

Java

EditText etPlace = (EditText) autocompleteFragment.getView().findViewById(R.id.places_autocomplete_search_input);
etPlace.setHint(place.getAddress())

科特琳

val etPlace = autocompleteFragment.view?.findViewById(R.id.places_autocomplete_search_input) as EditText
etPlace.hint = place.address

答案 1 :(得分:1)

这是我正在使用的代码,它工作正常。

对build.gradle(应用级别)进行一些更改

将其添加到build.gradle:

android{
   ...
   ext {
        googlePlayServicesVersion = "15.0.1"
   }
}

添加这些依赖项:

dependencies {
    ...
    //Also if you're using any firebase dependencies make sure that the are up to date
    implementation 'com.google.android.gms:play-services-places:16.0.0'
    implementation 'com.google.android.libraries.places:places:1.1.0'
}
apply plugin: 'com.google.gms.google-services'

在xml布局中:

<fragment
                android:id="@+id/autocomplete_fragment"
                android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

活动中的代码:

private void initGooglePlacesApi() {
        // Initialize Places.
        Places.initialize(getApplicationContext(), "YOUR_API_KEY");
        // Create a new Places client instance.
        PlacesClient placesClient = Places.createClient(getApplicationContext());

        // Initialize the AutocompleteSupportFragment.
        AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
                getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);

        autocompleteFragment.setHint(getString(R.string.select_location_search_bar));
//        autocompleteFragment.setLocationRestriction(RectangularBounds.newInstance(
//                new LatLng(34.7006096, 19.2477876),
//                new LatLng(41.7488862, 29.7296986))); //Greece bounds
        autocompleteFragment.setCountry("gr");


        // Specify the types of place data to return.
        autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ADDRESS, Place.Field.ADDRESS_COMPONENTS));
        autocompleteFragment.setTypeFilter(TypeFilter.ADDRESS);


        // Set up a PlaceSelectionListener to handle the response.
        autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(Place place) {
                if(place.getAddressComponents().asList().get(0).getTypes().get(0).equalsIgnoreCase("route")){
                    binding.textViewLocation.setText(place.getAddress()); //Works well
                    location = place.getAddress();

                }else{ //If user does not choose a specific place.
                    AndroidUtils.vibratePhone(getApplication(), 200);
                    TastyToast.makeText(getApplicationContext(),
                            getString(R.string.choose_an_address), TastyToast.DEFAULT, TastyToast.CONFUSING);
                }

                Log.i(TAG, "Place: " + place.getAddressComponents().asList().get(0).getTypes().get(0) + ", " + place.getId() + ", " + place.getAddress());
            }

            @Override
            public void onError(Status status) {
                Log.i(TAG, "An error occurred: " + status);
            }
        });
    }

答案 2 :(得分:0)

我相信这是一个错误,因为让它像这样工作毫无意义。 起作用的是设置其他自动填充的文本,而不是其自身的文本。 这一定是个错误。

答案 3 :(得分:0)

编辑:这是我的最新答案

# When you are using AutocompleteSupportFragment or AutocompleteActivity
# in Fragments, do this:
public class YourFragment extends Fragment {
/.../
@Override
public void onActivityResult (int requestCode,int resultCode,
 @Nullable Intent data){
 # AUTOCOMPLETE_REQUEST_CODE is just a unique constant, define it
 if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
   if (resultCode == AutocompleteActivity.RESULT_OK) {
    Place place = Autocomplete.getPlaceFromIntent(data);
    // when resultcode is RESULT_OK
    mAddressEditText.setText(place.getName());
    // Notice this line, update your editText up here
    }else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
    Status status = Autocomplete.getStatusFromIntent(data);
    // Handle error
    } else if (resultCode == AutocompleteActivity.RESULT_CANCELED) {
    // Handle results if canceled
    }
  super.onActivityResult(requestCode, resultCode, data);
 }
}
/.../
}
# If you are extending AppCompatActivity, you might want to do this
# ONLY when you are already doing something in onActivityResult
public class YourActivity extends AppCompatActivity{
/.../
@Override
public void onActivityResult (int requestCode,int resultCode,@Nullable Intent data){
 # your logic here.
 /.../ 
  # if you are already overriding onActivityResult, 
  # do not forget to put this line
  super.onActivityResult(requestCode, resultCode, data);
 }
/.../
}

我也遇到了问题。事实证明,您必须进行覆盖并以任何方式实施(如果您正在使用“片段”,则使用AutocompleteSupportFragmentAutocompleteActivity

如果您正在使用AppCompatActivity,则不必实现它,但是,如果您已经在onActivityResult之上进行某些操作,请不要忘记调用基本方法super.onActivityResult

答案 4 :(得分:0)

我找到了一个非常简单的解决方案。.只要在EditText中设置文本后稍稍延迟一下。因此,在您的PlaceSelectionListener中就可以这样:

th, td{border:1px solid red;}
.table-responsive>.table{padding:0 32px 0 24px;}
mat-expansion-panel-header .table td{text-align: center}

PS:这是kotlin代码,但在Java中几乎相似

答案 5 :(得分:0)

获取AutoCompleteFragment的引用,然后将文本设置为自动完成片段 像

autoCompleteFragment.setText("Address")

供参考,您可以查看文档

https://developers.google.com/android/reference/com/google/android/gms/location/places/ui/PlaceAutocompleteFragment

答案 6 :(得分:0)

我尝试了CacheMeOutside's solution,但是根本没有用。因此,我决定尝试Matthias's solution并成功了,因为文本实际上已经设置好,然后由于某种原因立即被删除。稍稍延迟即可解决。延迟可以短至1毫秒。

如果我的解决方案不适合您,则可以尝试尝试延迟。似乎它也不会停止视图渲染,因此您可以随时设置。

private lateinit var autocomplete: AutocompleteSupportFragment

        override fun onPlaceSelected(place: Place) {
            Timer("SetAddress", false).schedule(1) {
                autocomplete.setText(place.address)
            }
        }

Kotlin中的代码段。如果您的代码使用Java,则只需找到某种工具来延迟代码执行一段时间即可。

答案 7 :(得分:0)

马蒂亚斯(Matthias)的解决方案正在工作,但是在科特林。下面是Java中的相同实现

 @Override
 public void onPlaceSelected(Place place) {


                        String name =  place.getName()+", "+place.getAddress();
                      
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                autocompleteFragmentDestination.setText(name);
                            }
                        },300);

                    }

答案 8 :(得分:0)

简单。使用 Spannable 字符串设置颜色!

    val autocompleteFragment = childFragmentManager.findFragmentById(R.id.location_filter_autocomplete) as AutocompleteSupportFragment
    val text = SpannableString("Enter a location")
    text.setSpan(ForegroundColorSpan(Color.BLACK), 0, text.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
    autocompleteFragment.setHint(text)

在 onPlaceSelectedListener() 内部做同样的事情