在操作栏上添加搜索图标android [英] Add search icon on action bar android

查看:96
本文介绍了在操作栏上添加搜索图标android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们.

我在操作栏中有一个搜索图标,如下图所示

I have a search icon in action bar as image below

单击后,我希望操作栏更改为editText并在editText

When it is clicked, I want the action bar change to editText and has a search icon beside the editText

我知道如何用图像制作editText,但是如何将editText放在下面的图像之类的操作栏上?而且,一旦editText填充了值,我想使数据显示值.我应该使用意图吗?

I know how to make an editText with an image, but how to put the editText on the action bar like image below ? And I want to make the data display value once the editText has filled with value. Should I use intent ?

这是我到目前为止尝试过的.

This is what I've tried so far.

活动A

 getData(deviceName, month); // retrieve data from `MySQL` and load into listView

        @Override
         public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.create_menu, menu); 
            return true;
        }

        @Override
        public boolean onPrepareOptionsMenu(Menu menu) {
            mSearchAction = menu.findItem(R.id.search);
            return super.onPrepareOptionsMenu(menu);
        }

        @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.
            switch (item.getItemId()) {

                case R.id.search: // should I need to add intent ?
                    handleMenuSearch();
                    return true;

                case R.id.add: // create new file
                    View menuItemView = findViewById(R.id.add);
                    PopupMenu po = new PopupMenu(HomePage.this, menuItemView); //for drop-down menu
                    po.getMenuInflater().inflate(R.menu.popup_menu, po.getMenu());
                    po.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                        public boolean onMenuItemClick(MenuItem item) {
                            //  Toast.makeText(MainActivity.this, "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
                            if ("Create New File".equals(item.getTitle())) {
                                Intent intent = new Intent(HomePage.this, Information.class);  // go to Information class
                                startActivity(intent);

                            } else if ("Edit File".equals(item.getTitle())) {
                                Intent intent = new Intent(HomePage.this, Edit.class);
                                startActivity(intent);
                            }
                            return true;
                        }
                    });
                    po.show(); //showing popup menu

            }
            return super.onOptionsItemSelected(item);
        }

        protected void handleMenuSearch(){
            ActionBar action = getSupportActionBar(); //get the actionbar

            if(isSearchOpened){ //test if the search is open

                action.setDisplayShowCustomEnabled(false); //disable a custom view inside the actionbar
                action.setDisplayShowTitleEnabled(true); //show the title in the action bar

                //hides the keyboard
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(edtSeach.getWindowToken(), 0);

                //add the search icon in the action bar
                mSearchAction.setIcon(getResources().getDrawable(R.mipmap.search));

                isSearchOpened = false;
            } else { //open the search entry

                action.setDisplayShowCustomEnabled(true); //enable it to display a
                // custom view in the action bar.
                action.setCustomView(R.layout.search_bar);//add the custom view
                action.setDisplayShowTitleEnabled(false); //hide the title

                edtSeach = (EditText)action.getCustomView().findViewById(R.id.edtSearch); //the text editor

                //this is a listener to do a search when the user clicks on search button
                edtSeach.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                    @Override
                    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                            doSearch();
                            return true;
                        }
                        return false;
                    }
                });


                edtSeach.requestFocus();

                //open the keyboard focused in the edtSearch
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(edtSeach, InputMethodManager.SHOW_IMPLICIT);


                //add the close icon
                mSearchAction.setIcon(getResources().getDrawable(R.mipmap.search));

                isSearchOpened = true;
            }
        }

      @Override
        public void onBackPressed() {
            if(isSearchOpened) {
                handleMenuSearch();
                return;
            }
            super.onBackPressed();
        }

        private void doSearch() {
    //
        }


    }

活动A的屏幕截图

当按下搜索图标时,它原本打算进入另一个页面,并在editText上有一个搜索图标(与第二张图片完全一样),但是没有.它保留在同一页面中.

When search icon is pressed, it supposed to intent to another page and have a search icon on the editText (exactly like the second image), but it didn't. It remains in the same page.

这就是我想要的(来自微信)

This is what I want (From wechat)

在按下图标之前

按下后

推荐答案

该小部件名称为:android.support.v7.widget.SearchView

http://developer.android.com/reference/android/widget/SearchView.html

在菜单中:

 <item
        android:id="@+id/action_search"
        android:icon="@drawable/abc_ic_search_api_mtrl_alpha"
        android:title="@string/srch"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="ifRoom|collapseActionView" />

Java:

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        // Retrieve the SearchView and plug it into SearchManager
        final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
        SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

        return true;
    }

这完全类似于MaterialDesign图标.

android:icon="@drawable/abc_ic_search_api_mtrl_alpha"

这篇关于在操作栏上添加搜索图标android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆