简体   繁体   English

ActionBar中的Android SearchView

[英]Android SearchView in ActionBar

I searched for a solution, and tried many solutions from other questions but I did not find any that works. 我搜索了一个解决方案,并尝试了其他问题的许多解决方案,但没有找到任何可行的解决方案。 I want my SearchView to be shown in my ActionBar. 我希望我的SearchView显示在我的ActionBar中。 But it is only shown in the overflow menu (the three dots). 但这仅显示在溢出菜单中(三个点)。 Mybe someone could show me my error/a solution. 我的某人可以告诉我我的错误/解决方案。

My menu_searchview.xml: 我的menu_searchview.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_search"
    android:icon="@drawable/ic_magnify_white_24dp"
    android:title="Search"
    app:showAsAction="always"
    android:actionViewClass="android.widget.SearchView"/>
</menu>

My Activity: 我的活动:

import android.app.Activity;
import android.support.v4.view.MenuItemCompat;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SearchView;
import de.trainer.R;

public class SearchCategoryActivity extends Activity {

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

    @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_searchview, menu);

        MenuItem searchItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

This work fine for me: 这项工作对我来说很好:

The menu file: 菜单文件:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
            android:id="@+id/menu_search"
            app:actionLayout="@layout/search_view"
            android:orderInCategory="0"
            app:showAsAction="always|collapseActionView"
            android:icon="@drawable/ic_action_search"
            android:title="Buscar"/>

</menu>

The layout/search_view: 布局/ search_view:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:padding="4dp"
              android:layout_height="wrap_content">

    <EditText
            android:id="@+id/etSearch"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:cursorVisible="true"
            android:imeOptions="actionDone"
            android:inputType="text"
            android:textColor="@android:color/white"
            android:textCursorDrawable="@null"/>

</RelativeLayout>

In your Activity: 在您的活动中:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        try {
            menu.clear();
            inflater.inflate(R.menu.your_menu, menu);

            RelativeLayout actionView = (RelativeLayout) menu.findItem(R.id.menu_search).getActionView();
            final EditText editText = (EditText) actionView.findViewById(R.id.etSearch);
            //final EditText editText = (EditText) menu.findItem(R.id.menu_search).getActionView();
            editText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (null != activityAdapter) {
                        activityAdapter.getFilter().filter(s);
                    }
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });

            MenuItem menuItem = menu.findItem(R.id.menu_search);
            MenuItemCompat.setOnActionExpandListener(menuItem, new MenuItemCompat.OnActionExpandListener() {
                @Override
                public boolean onMenuItemActionExpand(MenuItem item) {
                    editText.requestFocus();

                    ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
                    return true; // Return true to expand action view
                }

                @Override
                public boolean onMenuItemActionCollapse(MenuItem item) {
                    // Do something when collapsed

                    // Borramos el texto que había escrito.
                    editText.setText("");

                    return true; // Return true to collapse action view
                }
            });
        } catch (Exception ex) {
            logManager.error("OCOM - Exception: " + ex.toString());
        }
        super.onCreateOptionsMenu(menu, inflater);
    }

Finally, in your adapter you should implements the Filterable interfaz. 最后,在您的适配器中,您应该实现Filterable interfaz。

Hope it helps you!! 希望对您有帮助!!

将android:actionViewClass =“ android.widget.SearchView”替换为app:actionViewClass =“ android.support.v7.widget.SearchView”

Try 尝试

app:actionViewClass="android.widget.SearchView" or app:actionViewClass =“ android.widget.SearchView”

app:actionViewClass="android.support.v7.widget.SearchView" app:actionViewClass =“ android.support.v7.widget.SearchView”

instead of 代替

android:actionViewClass="android.widget.SearchView" android:actionViewClass =“ android.widget.SearchView”

also you have to remove 你也必须删除

android:icon="@drawable/ic_magnify_white_24dp" and change following method android:icon =“ @ drawable / ic_magnify_white_24dp”并更改以下方法

 @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_searchview, menu);

            MenuItem searchItem = menu.findItem(R.id.action_search);
            SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
            searchView.setIconifiedByDefault(false);
            return true;
        }

I hope it solves your issue. 希望它能解决您的问题。

From your layout and Activity, I understand you are not using a Compatible library. 从您的布局和活动中,我知道您没有使用兼容库。 In that case, I recommend switching your AXML to 在这种情况下,建议您将AXML切换为

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
      <item android:id="@+id/action_search"
            android:icon="@drawable/ic_magnify_white_24dp"
            android:title="Search"
            android:showAsAction="always"
            android:actionViewClass="android.widget.SearchView"/>
</menu>

Note, the switch from app:showAsAction to android:showAsAction 注意,从app:showAsAction切换到android:showAsAction

This may be causing the issue. 这可能是导致问题的原因。

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM