操作栏中的AutoCompleteTextView不起作用?

时间:2015-05-13 18:48:50

标签: android android-actionbar autocompletetextview

我正在尝试在操作栏中使用自动完整文本视图。虽然我能够将自动完成操作视图放在那里,但每当我开始输入时,我都看不到有关建议的下拉菜单。它就像一个普通的编辑文本框。

然后,为了测试,我在应用程序布局中也包含了一个自动完整的文本视图。但是这个工作完全像它应该的那样。 两个自动完成文本视图都使用相同的适配器进行初始化。

这是主要活动的onCreate方法。

//defining an array to be used by the array adapters

import android.support.v4.app.FragmentActivity;
import android.support.v4.view.MenuCompat;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

private static final String[] COUNTRIES = new String[] { "Belgium",
    "France", "France_", "Italy", "Germany", "Spain" };

protected void onCreate(Bundle savedInstanceState) {

    //inflating the activity layout
    super.onCreate(savedInstanceState);
    setContentView(R.layout.frag_map);

    //getting the action bar
    actionBar = getSupportActionBar();
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    actionBar.setCustomView(R.layout.map_screen_ab_auto_commplete);
    LayoutInflater inflator = (LayoutInflater) this
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflator.inflate(R.layout.map_screen_ab_auto_commplete, null);

    //Defining an adapter which will then be used by auto complete text view
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_dropdown_item_1line, COUNTRIES);

    //setting an auto complete text view in the action bar.PLEASE NOTE THAT THIS DOES NOT WORK
        mAutocompleteView = (AutoCompleteTextView) v
        .findViewById(R.id.map_screen_auto_complete);
    mAutocompleteView.setAdapter(adapter);

    //just for checking, another auto complete text view was added. THIS WORKS
    test_ac_view = (AutoCompleteTextView) this.findViewById(R.id.test_ac_view);
    test_ac_view.setAdapter( adapter);
}

这是同时包含自动完整文字视图的主要活动布局:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <AutoCompleteTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="New AutoCompleteTextView"
        android:id="@+id/test_ac_view"
        android:layout_gravity="center_horizontal" />

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment"
         />

</LinearLayout>

这是包含自动完整文字视图的操作栏布局。

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal">

        <AutoCompleteTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/map_screen_auto_complete"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:hint="Search Location" />
    </LinearLayout>
</LinearLayout>

0 个答案:

没有答案