【问题标题】:Inflating an AutoCompleteSupportFragement in an editText onClick在 editText onClick 中膨胀 AutoCompleteSupportFragment
【发布时间】:2019-10-01 16:08:04
【问题描述】:

我正在尝试在 editText 的 onClick 事件中扩充使用 Google Places API 的 AutoCompleteSupportFragment。

我想在点击editText时为该片段充气,以便用户可以输入一个位置,从自动完成片段中的建议中选择,位置名称将显示在editText上。

我检查了这个链接here,但它对我不起作用。

places_auto_complete_fragment.xml

    <?xml version="1.0" encoding="utf-8"?>
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/place_autocomplete_fragment"
    android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment" />

我是如何充气的

sourceEditText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            LayoutInflater layoutInflater = getLayoutInflater();
            layoutInflater.inflate(R.layout.places_auto_complete_fragment,addTripLinearLayout,false);
        }
    });

感谢任何帮助。谢谢

【问题讨论】:

  • 工藤的@arnavJJ

标签: android android-fragments android-edittext google-places-api onclicklistener


【解决方案1】:
  1. AutocompleteSupportFragment 向用户呈现一个搜索框按钮,点击时呈现一个搜索框 UI
  2. 膨胀布局会加载 UI,但您必须将其添加到视图中才能使其可见。

【讨论】:

    【解决方案2】:

    在editText中使用Intent builder代替片段......

    在 Oncreate 上初始化

       // Initialize Places.
       Places.initialize(getApplicationContext(), "***YOUR API KEY***");
    
       // Create a new Places client instance.
       PlacesClient placesClient = Places.createClient(this);
    

    然后在点击事件的edittext上使用下面的代码...

    List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME);
    // Start the autocomplete intent.
    Intent intent = new Autocomplete.IntentBuilder(AutocompleteActivityMode.FULLSCREEN, fields).build(this);
    startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                Place place = Autocomplete.getPlaceFromIntent(data);
                Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
            } else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
                // TODO: Handle the error.
                Status status = Autocomplete.getStatusFromIntent(data);
                Log.i(TAG, status.getStatusMessage());
            } else if (resultCode == RESULT_CANCELED) {
                // The user canceled the operation.
            }
        }
    }
    

    请参考以下链接https://stackoverflow.com/a/55045772/10579969...

    如果您只使用片段,请参考以下代码

           AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
                    getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);
        autocompleteFragment.setCountry("IN");    //country type
        autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME)); 
       //to indicate the types of place data that you want to get.
    

    【讨论】:

    • 对我来说 (AutocompleteSupportFragment)getChildFragmentManager() 有效。 getSupportFragmentManager() 不起作用。
    猜你喜欢
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-28
    • 1970-01-01
    相关资源
    最近更新 更多