【问题标题】:Android : ActionView Collapses on pressing back buttonAndroid:按下后退按钮时 ActionView 折叠
【发布时间】:2013-02-11 01:28:48
【问题描述】:

我正在使用 ActionBarSherlock 库来获取预 Honeycomb 版本的操作栏。我有一个活动,其操作栏菜单从 xml 下方膨胀

menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">        
    <item android:id="@+id/action_bar_search" 
        android:icon="@drawable/ic_search"
        android:showAsAction="always|collapseActionView" android:title="Search"
        android:actionLayout="@layout/layout_search">        
    </item> 
</menu>

下面是actionLayout

> layout_search.xml

<?xml version="1.0" encoding="utf-8"?>
   <AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/autoCompText_action_bar_search"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"    
    android:cursorVisible="true"    
    android:imeOptions="flagNoExtractUi"
    android:inputType="text"
    android:textColor="@color/color_action_bar_text"
    android:textCursorDrawable="@android:color/black" 
    android:background="@drawable/textfield_bg_activated_holo_dark"            
    />

我还实现了 OnActionExpandListener 来监听展开和折叠 菜单项的事件。下面是我对 OnActionExpandListener 的实现

  private OnActionExpandListener searchActionExpandListener = new OnActionExpandListener() {        
            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {              

                /* This is done so that requestFocus() can popup the softkeyboard. 
                 * Else, no softkeyboard is popped up
                 */
                edtTextSearch.post(new Runnable() {
                    @Override
                    public void run() {
                        edtTextSearch.requestFocus();
                        mImm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        mImm.showSoftInput(edtTextSearch, InputMethodManager.SHOW_IMPLICIT);
                    }
                });
                return true;
            }
        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {                
            mImm.hideSoftInputFromWindow(edtTextSearch.getWindowToken(), 0);
            return true;
        }
    };

所以现在,按下操作栏上的搜索按钮时,会显示 actionLayout,并且会弹出软键盘并聚焦在它上面。到目前为止一切正常。但是当我按下返回键(手机上的硬键)时,动作视图就会崩溃。我要做的就是在按下后退键时隐藏软键盘(如果它正在显示)而不折叠动作视图。谁能帮帮我?

【问题讨论】:

  • 嗨,Viren,您还应该看看我对类似问题的回答:Contextual ActionBar hides when I click on hardware backbutton and the keyboard is out,它可能会对您有所帮助。
  • 感谢 Filo 如此积极的回应。你的链接给了我一个全新的视角
  • 很高兴为您提供帮助!这很奇怪,因为我从未在我的项目中看到过这种行为。我会做一些测试,以了解我的错误/正确之处。无论如何,我希望这会对你有所帮助。良好的编码。

标签: android actionbarsherlock back-button


【解决方案1】:

ActionBarSherlockCollapseActionView 也有类似的问题。您的代码对我帮助很大,但我无法使用Runnable()(不知道为什么)在早期版本(如 2.+)上弹出软键盘。只是一个小技巧可能会避免您使用这种方式,请参阅下面的代码:

@Override
public boolean onMenuItemActionExpand(MenuItem item) {              
    // Set the focus
    edtTextSearch.requestFocus();  
    // This force the Soft Keyboard to appear whatever the version used
    mImm.toggleSoftInput(InputMethodManager.SHOW_FORCED,  
                                   InputMethodManager.HIDE_IMPLICIT_ONLY);
    return true;
}  

这在 2.+ 及更高版本中完美运行。现在,根据您的要求,我没有在所有经过测试的设备上看到这些东西。当View 展开时,会出现软键盘。然后当我按下后退按钮时,软键盘消失,View 不会折叠(即使在横向模式下)。如果我再按一次,这里View 崩溃。
你能解释更多你想要什么,因为我没有正确看到你在后退按钮上折叠View 的操作吗?

经过一些研究,我认为您想通过 CollapseActionView 保持您的 DownDropList 可见,因此查看以下内容可能会很有用:DropDownList of AutoCompleteTextView opened after pressing the Back key?。以防万一,我发现了这个:EditText with soft keyboard and “Back” button

【讨论】: