Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android操作栏上添加搜索图标_Android_Search_Android Actionbar_Icons - Fatal编程技术网

在android操作栏上添加搜索图标

在android操作栏上添加搜索图标,android,search,android-actionbar,icons,Android,Search,Android Actionbar,Icons,大家好 我在操作栏中有一个搜索图标,如下图所示 单击时,我希望操作栏更改为editText,并在editText 我知道如何使用图像制作editText,但如何将editText像下面的图像一样放在操作栏上?我想在editText中填充值后,让数据显示值。我应该使用意图吗 这就是我到目前为止所尝试的 活动A getData(deviceName, month); // retrieve data from `MySQL` and load into listView @O

大家好

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

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

我知道如何使用图像制作editText,但如何将editText像下面的图像一样放在操作栏上?我想在editText中填充值后,让数据显示值。我应该使用意图吗

这就是我到目前为止所尝试的

活动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的屏幕截图

 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() {
    //
        }


    }

当按下搜索图标时,它应该指向另一个页面,并且在editText上有一个搜索图标(与第二个图像完全相同),但它没有。它仍在同一页中

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

在按下图标之前

压制后


您应该使用内置的android SearchView,根据android用户界面模式,这一点很好

menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".ui.home.activities.TransactionSearchActivity">
    <item
        android:id="@+id/action_search"
        android:icon="@android:drawable/ic_menu_search"
        android:title="@string/search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="ifRoom" />
</menu>

确保膨胀菜单:D

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

在您的菜单中:

 <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" />
 @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"

你能再看看我的帖子吗?我应该使用intent吗?在使用此选项菜单的同一活动中更新数据。此操作不需要任何意图。因此,当单击后退按钮(编辑文本旁边)时,它将显示一月列表视图?而不是获取“searchPlate”编辑文本并使用
searchPlate.setHint(“搜索”)
,您只需使用
searchView.setQueryHint(“Search”).Sir我应该在单击搜索图标时使用intent,以便在editText填充值时显示相关数据吗?这取决于您的期望,在我的回答中,您可以使用intent而不使用intent。android:icon=“@android:drawable/ic\U menu\u search”如果您已经更新到Android X,请在XML和Java类中使用androidx.appcompat.widget.SearchView。