我想在工具栏中实现Autocompletetextview

时间:2016-12-14 05:46:07

标签: android android-toolbar android-tabs autocompletetextview

这是我的代码。我正在使用menu_search来显示搜索图标。 Search_layout正在显示autocompletetextview。我不知道在mainactivity中初始化autocompletetextview的位置。

menu_search.xml         

        <item android:id="@+id/action_search"
            android:name="search"
            android:title="search"
            android:imeOptions="actionSearch"
            android:inputType="textAutoComplete|textAutoCorrect"
            app:actionLayout="@layout/search_layout"
            android:icon="@android:drawable/ic_menu_search"
            app:showAsAction="ifRoom|collapseActionView"/>

    </menu>

search_layout.xml                  

        <AutoCompleteTextView
            android:id="@+id/autoCompleteTextView_searchWidget"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:textColor="@color/colorAccent"
            android:imeOptions="actionSearch"
            android:inputType="text">
            <requestFocus/>
        </AutoCompleteTextView>

    </LinearLayout>

MainActivity.java

    public class MainActivity extends AppCompatActivity {
    private Toolbar toolbar;
        private TabLayout tabLayout;
        private ViewPager viewPager;

      @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            //getSupportActionBar().setDisplayHomeAsUpEnabled(true);

            viewPager = (ViewPager) findViewById(R.id.viewpager);
            setupViewPager(viewPager);

            tabLayout = (TabLayout) findViewById(R.id.tabs);
            tabLayout.setupWithViewPager(viewPager);
            //setupTabIcons();
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {

            getMenuInflater().inflate(R.menu.menu_search,menu);
            return super.onCreateOptionsMenu(menu);
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            return super.onOptionsItemSelected(item);
        }
}

2 个答案:

答案 0 :(得分:0)

fgets

将工具栏添加到操作栏

<android.support.v7.widget.Toolbar       
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="@dimen/_40sdp"
android:background="@color/color_white"
android:contentInsetEnd="0dp"
android:contentInsetRight="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/ivShorting"
        android:layout_toRightOf="@+id/ivLeftIcon"
        android:background="@drawable/search_drawable"
        android:drawableLeft="@drawable/ic_search"
        android:drawablePadding="@dimen/_10sdp"
        android:gravity="left|center_vertical"
        android:hint="Search Job"
        android:imeOptions="actionSearch"
        android:inputType="textAutoComplete|textAutoCorrect"
        android:lines="1"
        android:textColor="@color/color_outer_space"
        android:textColorHint="@color/color_outer_space"
        android:textSize="@dimen/_14sdp"/>
</RelativeLayout>

按照下面的链接将ArrayList添加到AutocompleteTextView

http://androidexample.com/Show_AutoComplete_Suggestions_-_Android_Example/index.php?view=article_discription&aid=105

答案 1 :(得分:0)

我建议你可以使用MaterialSearchView

<FrameLayout
        android:id="@+id/toolbar_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/theme_primary" />

        <com.miguelcatalan.materialsearchview.MaterialSearchView
            android:id="@+id/search_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </FrameLayout>

https://github.com/MiguelCatalan/MaterialSearchView

https://github.com/Mauker1/MaterialSearchView

http://joerichard.net/android/android-material-design-searchview-example/