简体   繁体   English

软键盘上的搜索键问题

[英]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. 我在操作栏中使用搜索字段,方法是将其转换为布局文件夹中的xml(称为search_layout.xml)。在2.3.3版本的android中,出现了诸如搜索图标未显示在键盘上的问题输入文字后的时间。 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. 在更高版本(如4.0.4和4.4.2 android)中不会发生这种情况。

Here is the search_layout.xml: 这是search_layout.xml:

And this xml is reffererd to in res>>menu>>menu.xml 并在res >> menu >> menu.xml中引用此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. 这是我在称为Activity_HomeScreen类的活动中为此使用的代码段。

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;
            }

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

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