Android 的 Places SDK:如何验证 AutocompleteSupportFragment 不为空?

Places SDK for Android: How to validate AutocompleteSupportFragment is not empty?

我在 AlertDialog 中添加了一个 AutocompleteSupportFragment。
AlertDialog
我想确保位置字段在用户单击 "Create" 按钮后不为空。 (就像我确保其他字段不为空一样。)
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);
            }
        });

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

但是,在 Google 的问题跟踪器中有一个相关的功能请求公开 onClearListener(),我建议加注星标以提高知名度和订阅通知:

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

此外,其中一条评论中提到了 "hack" 或变通方法,以获得清除按钮的 View。请注意,对于 AutocompleteSupportFragment,按钮的 ID 是 places_autocomplete_clear_button.

例如,您可以创建一个 class 变量:

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();
        }
    });

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

我已经回答了这个 。你可以找到片段里面的清除按钮,听听。

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是否为空。