简体   繁体   中英

Issue with search key on soft keyboard

I am using a search field in the actionbar by reffering it to an xml in the layout folder called search_layout.xml.In the 2.3.3 version of android, there is an issue like the search icon is not shown in the keypad for the first time after I enter the text. Instead I have to click to the "enter" key, then on the textfield and then only the "search" icon appears. This does not happen in higher versions like 4.0.4 and 4.4.2 of android.

Here is the search_layout.xml:

And this xml is reffererd to in res>>menu>>menu.xml

<item
    android:id="@+id/action_notification"
    android:actionLayout="@layout/search_layout"
    android:icon="@drawable/ic_searchicon"
    android:orderInCategory="0"
    android:showAsAction="always|collapseActionView"
    android:title="Search"/>

This is the code snippet I am using for this in my activity called Activity_HomeScreen class.

public boolean onCreateOptionsMenu(Menu menu) 
    {
        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        MenuItem itemListMap,itemRefresh;

        editsearch = (EditText) menu.findItem(R.id.action_notification).getActionView();
        editsearch.addTextChangedListener(textWatcher);
        editsearch.setOnEditorActionListener(new OnEditorActionListener() 
        {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) 
            {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) 
                {
                    editsearch.clearFocus();
                    InputMethodManager imm = (InputMethodManager)Activity_HomeScreen.this.getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(editsearch.getWindowToken(), 0);
                    captureViepagerFragments(mVpContainer.getCurrentItem());
                }
                return false;
            }
        });
 MenuItem menuSearch = menu.findItem(R.id.action_notification);

        menuSearch.setOnActionExpandListener(new OnActionExpandListener() 
        {

            // Menu Action Collapse
            @Override
            public boolean onMenuItemActionCollapse(MenuItem item)
            {
                // Empty EditText to remove text filtering
                editsearch.setText("");
                editsearch.clearFocus();
                InputMethodManager imm = (InputMethodManager)Activity_HomeScreen.this.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(editsearch.getWindowToken(), 0);
                captureViepagerFragments(mVpContainer.getCurrentItem());
                return true;
            }

            // Menu Action Expand
            @Override
            public boolean onMenuItemActionExpand(MenuItem item) 
            {
                // Focus on EditText
                editsearch.requestFocus();
                // Force the keyboard to show on EditText focus
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
                return true;
            }
        });


        return super.onCreateOptionsMenu(menu);
    }

Solved this issue by doing like this:

            public boolean onMenuItemActionExpand(MenuItem item) 
            {
                editsearch.clearFocus();
                editsearch.post(new Runnable() {
                    @Override
                    public void run() {
                        editsearch.requestFocus();
                        final InputMethodManager imm = (InputMethodManager) Activity_Cat_Products.this
                                .getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.showSoftInput(editsearch, InputMethodManager.SHOW_IMPLICIT);

                    }
                });
                return true;
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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