使用EditText insead SearchView

时间:2014-01-11 18:59:06

标签: android android-edittext searchview

我正在尝试在此代码中使用EditText而不是SearchView

  private void setupSearchView() {
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    final SearchView searchView = (SearchView) findViewById(R.id.searchView);
    SearchableInfo searchableInfo = searchManager.getSearchableInfo(getComponentName());
    searchView.setSearchableInfo(searchableInfo);
}

我想要使用的布局是

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SearchViewActivity" >


<EditText android:id="@+id/searchViewET"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"/>

<TextView
    android:id="@+id/searchViewResult"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/searchView"
    android:layout_centerHorizontal="true"
    android:text="@string/hello_world" />

</RelativeLayout>

那是因为我使用低api级别(级别8)而SearchView使我的应用程序崩溃。 怎么做? 在此先感谢。

2 个答案:

答案 0 :(得分:3)

在xml中你使用edittext而在程序中你使用searchview ??如果您使用edittext作为搜索,我不会完全解决您的代码问题,那么您还应该获取edittext的视图,然后将它设置为edittext的onTextChangeListener()以搜索项目

答案 1 :(得分:0)

Android Support Library App Compat Library将操作栏功能(包括SearchView)反向回API7 +设备。然后,您可以为Action Bar guide添加SearchView,并使用您通常在较新设备上使用的所有方法。