【问题标题】:Places SDK for Android: How to validate AutocompleteSupportFragment is not empty?Places SDK for Android:如何验证 AutocompleteSupportFragment 是否为空?
【发布时间】:2020-03-30 23:49:12
【问题描述】:

我在 AlertDialog 中添加了一个 AutocompleteSupportFragment。
AlertDialog
我想确保用户单击“创建”按钮后位置字段不为空。 (就像我如何确保其他字段不为空一样。)
Fields validation
我希望有类似 setOnPlaceUnselectedListener 的东西。但是 AutocompleteSupportFragment 只有 setOnPlaceSelectedListener,这并不理想,因为它不知道用户是否输入了某个位置然后清除输入。

AutocompleteSupportFragment 确实有一个 setText 方法来设置要在搜索输入字段中显示的文本。但是好像没有对应的getText方法。

是否有验证 AutocompleteSupportFragment 是否选择了 Place 或是否为空? .xml

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

Activity.java


autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(Place place) {
                // Updating my class variables here doesn't help. 
                // If user first selects a place, then clears it and click "Create", 
                // I won't know that the location field is empty.
                Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
            }

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

【问题讨论】:

    标签: android google-maps android-fragments google-places-api


    【解决方案1】:

    您的理解是正确的,目前没有“setOnPlaceUnselectedListener”或“getText”或任何其他通过 Places SDK 的方法可以帮助您验证输入是否为空。

    但是,在 Google 的问题跟踪器中公开onClearListener() 的相关功能请求,我建议加星以增加可见性并订阅通知:

    https://issuetracker.google.com/issues/35828573

    此外,在其中一个 cmets 中提到了一个“hack”或解决方法来获取清除按钮的 View。请注意,对于 AutocompleteSupportFragment,按钮的 id 是 places_autocomplete_clear_button

    例如,你可以创建一个类变量:

    private String selectedPlace = null;
    

    并将其用作选择或取消选择地点的标志。

        autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
    
            @Override
            public void onPlaceSelected(Place place) {
                selectedPlace = place.getName();
            }
    
            @Override
            public void onError(Status status) {
                selectedPlace = null;
            }
    
        });
    
        View clearButton = autocompleteFragment.getView().findViewById(R.id.places_autocomplete_clear_button);
        clearButton.setOnClickListener(view -> {
            autocompleteFragment.setText("");
            selectedPlace = null;
        });
    
        final Button button = findViewById(R.id.button_id);
        button.setOnClickListener(v -> {
            if (selectedPlace == null) {
                Toast.makeText(getApplicationContext(), "Location cannot be empty", Toast.LENGTH_LONG).show();
            }
        });
    

    我已经对此进行了测试,因此我可以确认它有效,我希望它也可以帮助你!

    【讨论】:

    • onClearListener() 正是我所需要的!我已经盯着这个问题了——不敢相信它被标记为 p4。非常感谢您的解决方法 - 它确实有效!
    • 很高兴听到并乐于提供帮助! :)
    • 我在尝试您建议的 hack 时遇到此错误。 Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
    • 您能发布您的(可重现的)代码吗?最好在一个新问题中。我去看看。
    【解决方案2】:

    我已经回答了这个here。您可以在片段中找到清除按钮并收听。

    autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(Place place) {
            homeAddressPlaceID = place.getId();
            Log.i(TAG, "Place: " + place.getName() + ", " + place);
    
            autocompleteFragment.getView().findViewById(R.id.places_autocomplete_clear_button)
                    .setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            autocompleteFragment.setText("");
                            homeAddressPlaceID = "";
                        }
                    });
        }
    
        @Override
        public void onError(Status status) {
            Log.i(TAG, "An error occurred: " + status);
        }
    });
    

    之后,你可以检查homeAddressPlaceID是否为空。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多