使用Kotlin设置AutocompleteSupportFragment样式

问题描述 投票:0回答:1

我正在尝试更改我的AutocompleteSupportFragment字段的样式

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/llSearchHolder"
    android:padding="7dp">
    <fragment android:id="@+id/autocomplete_fragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
        android:hint="@string/Iam_going_to"
    />
</LinearLayout>

我尝试实现answers on this page,但始终会出现Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.EditText错误。我正在使用Kotlin,所以我的代码如下所示:

val autocompleteFragment = supportFragmentManager.findFragmentById(R.id.autocomplete_fragment) as AutocompleteSupportFragment?
autocompleteFragment!!.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME));
((autocompleteFragment.getView()!!.findViewById(R.id.autocomplete_fragment)) as EditText).textSize = 30.0f
android kotlin autocompletetextview
1个回答
0
投票

您需要使用以下代码

((autocompleteFragment.getView()!!.findViewById(R.id.places_autocomplete_search_input)) as EditText).textSize = 30.0f

EditText的正确ID是places_autocomplete_search_input而不是autocomplete_fragment


问题分析

您正在xml中使用片段com.google.android.libraries.places.widget.AutocompleteSupportFragment

[查看AutocompleteSupportFragment片段的代码时,您可以看到它使用布局places_autocomplete_fragment.xml。下面的代码

public class AutocompleteSupportFragment extends Fragment {
    ....

    public AutocompleteSupportFragment() {
        super(layout.places_autocomplete_fragment);
        ....
    }
}

现在,如果您查看places_autocomplete_fragment.xml,可以看到EditText的ID为places_autocomplete_search_input,下面的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:gravity="center"
    android:layoutDirection="locale"
    android:orientation="vertical"
    android:textDirection="locale">

  <ImageButton
      android:id="@+id/places_autocomplete_search_button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_weight="0"
      android:background="@null"
      android:contentDescription="@string/places_autocomplete_search_hint"
      android:padding="@dimen/places_autocomplete_button_padding"
      android:src="@drawable/quantum_ic_search_grey600_24" />

  <EditText
      android:id="@+id/places_autocomplete_search_input"
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1"
      android:background="@null"
      android:focusable="false"
      android:focusableInTouchMode="false"
      android:hint="@string/places_autocomplete_search_hint"
      android:inputType="textNoSuggestions"
      android:lines="1"
      android:maxLines="1"
      android:paddingLeft="@dimen/places_autocomplete_search_input_padding"
      android:paddingRight="@dimen/places_autocomplete_search_input_padding"
      android:singleLine="true"
      android:textColor="@color/places_autocomplete_search_text"
      android:textColorHint="@color/places_autocomplete_search_hint"
      android:textSize="@dimen/places_autocomplete_search_input_text" />

  <ImageButton
      android:id="@+id/places_autocomplete_clear_button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_weight="0"
      android:background="@null"
      android:contentDescription="@string/places_autocomplete_clear_button"
      android:padding="@dimen/places_autocomplete_button_padding"
      android:src="@drawable/quantum_ic_clear_grey600_24" />

</LinearLayout>
© www.soinside.com 2019 - 2024. All rights reserved.