问题描述
我使用的杰克沃顿商学院的优秀 ActionBarSherlock 的图书馆,有一个可折叠的搜索动作视图。我想弹出当搜索动作视图扩展了软键盘。
I am using Jake Wharton's excellent ActionBarSherlock library and have a collapsible search action view. I want to popup the soft keyboard when the search action view is expanded.
我们可以阅读使用DialogFragments这样的DialogFragment的推荐的方法一>由谷歌的博客文章(改变一个位可读性)。
We can read a recommended way of doing this in a DialogFragment in the "Using DialogFragments" blog post by Google (altered a bit for readability).
// Show soft keyboard automatically
mEditText.requestFocus();
int mode = WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE;
getDialog().getWindow().setSoftInputMode(mode);
似乎这不是扩大和要求集中了可折叠的EditText动作视图何时工作一>。我现在有这一点。
This does not seem to work when expanding and requesting focus on a collapsable EditText action view. I currently have this.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.my_activity, menu);
MenuItem menuItem = menu.findItem(R.id.menu_search);
final EditText searchText = (EditText) menuItem.getActionView();
menuItem.setOnActionExpandListener(new OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return true; // Return true to collapse action view
}
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
searchText.requestFocus();
int mode = WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE;
getWindow().setSoftInputMode(mode);
return true; // Return true to expand action view
}
});
}
下面是在my_activity菜单的xml文件我的菜单项的定义:
Here's my menu item definition in the my_activity menu xml file:
<item
android:id="@+id/menu_search"
android:icon="@drawable/ic_menu_search"
android:actionLayout="@layout/collapsible_edittext"
android:showAsAction="always|collapseActionView"
android:title="@string/menu_search"/>
...和collapsible_edittext动作布局
... and the collapsible_edittext action layout
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_search"
android:imeOptions="actionSearch"
android:inputType="text"
android:focusable="true"
android:focusableInTouchMode="true" />
警告:我不想的动力的软键盘。当用户具有一个硬件键盘请求焦点是足够的。
Caveat: I do not want to force the soft keyboard. When the user has a hardware keyboard requesting focus is sufficient.
任何想法的人?
推荐答案
也许不是requestFocus从来没有发生在code的问题,因为searchText没有扩大了吗?我不知道,反正......这个工作对我来说:
Maybe the requestFocus never occurred in the code in the question because the searchText was not expanded yet? I don't know, anyway... this worked for me:
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
searchText.post(new Runnable() {
@Override
public void run() {
searchText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(searchText, InputMethodManager.SHOW_IMPLICIT);
}
});
return true; // Return true to expand action view
}
这篇关于我怎么能专注于操作栏中的(当它已展开。)一个可折叠的动作视图的EditText项目和强制软键盘打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!