操作栏中的AutoCompleteTextView无法工作?

3
我正在尝试在操作栏中使用自动完成文本视图。虽然我能够将自动完成操作视图放在那里,但每当我开始输入时,我不会看到下拉菜单中的推荐内容。它就像一个普通的编辑文本框。
然后,只是为了测试,我也在应用程序布局中包含了一个自动完成文本视图。但这个可以正常工作。两个自动完成文本视图都使用相同的适配器进行初始化。
这是主Activity的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>

你在哪里定义了 test_ac_view - Petro
2个回答

0
在 Manifest 文件中,请确保 AppTheme 如下所示:
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">

0
问题在于您将适配器设置为使用Inflator创建的AutoCompleteTextView。但是您的ActionBar使用了另一个来自R.layout.layout_id的创建方式,这是通过setCustomView方法提供的。
请尝试执行以下操作:
LayoutInflater inflator = (LayoutInflater) this
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.map_screen_ab_auto_commplete, null);
actionBar.setCustomView(v);

使用 actionBar.setCustomView(R.layout.map_screen_ab_auto_commplete); 代替。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接