Android Yelp 类似于 ActionBar 中的搜索栏

标签 android android-layout

我正在尝试创建在搜索时采用多个参数的 android 应用程序。目前,我有一个可搜索的界面,可以从我的操作栏中进行搜索。但是,我想创建一个类似搜索栏的 yelp,我有 2 个文本字段可以输入数据,如图所示:

enter image description here

如何在搜索时添加额外的文本字段? 这是我用来初始化搜索的代码

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_home, menu);

    // Associate searchable configuration with the SearchView
    SearchManager searchManager =
            (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    final SearchView searchView =
            (SearchView) menu.findItem(R.id.action_search).getActionView();
    searchView.setSearchableInfo(
            searchManager.getSearchableInfo(getComponentName()));

    // set query change listener to hide keyboard after input has been
    // submitted
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }

        @Override
        public boolean onQueryTextSubmit(String query) {

            // hides and then unhides search tab to make sure keyboard
            // disappears when query is submitted
            searchView.clearFocus();
            return false;
        }
    });

这是我的菜单布局:

<item
    android:id="@+id/action_search"
    android:title="@string/search"
    android:orderInCategory="100"
    impulz:showAsAction="always"
    impulz:actionViewClass="android.widget.SearchView"/>

<item android:id="@+id/action_settings" android:title="@string/action_settings"
    android:orderInCategory="100" impulz:showAsAction="never" />

非常感谢。

最佳答案

你可以使用 actionBar.setCustomView(R.layout.custom_search); 并以编程方式设置actionBar“自定义 View ”

像这样:

custom_search.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="right"
    android:orientation="horizontal">
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"             
    android:orientation="vertical" >
    <com.actionbarsherlock.widget.SearchView
        android:id="@+id/search1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:iconifiedByDefault="false"
        android:visibility="invisible" />
   <com.actionbarsherlock.widget.SearchView
        android:id="@+id/search2"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:iconifiedByDefault="false"
        android:visibility="invisible" />
    </LinearLayout>
    <ImageButton
        android:id="@+id/btn_search_content"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:src="@drawable/search_content" />

</LinearLayout>

菜单.xml

<item
    android:id="@+id/menu_search"
    android:icon="@drawable/red_icon"
    android:showAsAction="always"
    android:title="search" />

在你的 onCreate() 中使用 setCustomView

主 Activity

@Override
public void onCreate(Bundle savedInstanceState) {
    // ...
    actionBar.setCustomView(R.layout.custom_search);
    actionBarCustomView = action_bar.getCustomView();
    searchView = ((SearchView)     actionBarCustomView.findViewById(R.id.search1));
    searchView2 = ((SearchView) actionBarCustomView.findViewById(R.id.search2));
    searchView.setOnCloseListener(new SearchView.OnCloseListener() {
        @Override
        public boolean onClose() {
            searchView.setVisibility(View.INVISIBLE);
            searchView2.setVisibility(View.INVISIBLE);
            return false;
        }
    });
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        case R.id.menu_search:
            //set visibilty 
           //do what ever you want
            break;

    }
    return true;
}

希望这对你有用。

关于Android Yelp 类似于 ActionBar 中的搜索栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31031881/

相关文章:

android - Google PlacePicker 在启动后立即关闭,结果代码为 2

android - Maps API 需要已设置的额外权限

android - 嵌套 ScrollView + TextView + RecyclerView

android - 更改无边框按钮的填充

android - 边距/填充缩小 ImageView

android - 无法在模拟器中设置实际设备分辨率

java - SensorEvent.timestamp 和 Location.getElapsedRealtimeNanos() 时间戳延迟偏移

android - 键盘在使用adjustPan时隐藏操作栏

android - 当纯色透明时,形状中的圆角会显示颜色

安卓发送邮件