【发布时间】:2013-12-13 05:52:14
【问题描述】:
我在ActionBar 上有一个带有菜单项的ActionView(使用ActionBarSherlock),我可以在其中显示一个EditText 作为搜索字段。这是在 ActionBar 中使用 CustomView 启动另一个 Activity 的输入,它显示相同的布局(我没有使用任何东西来强制 SoftKeyboard 出现在第二个活动中,这里没有问题)。当我想让软键盘在第一个活动中折叠时自动出现/消失时,我使用:
openKeyboard 方法
mImm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
closeKeyboard 方法
mImm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);
我使用ActionExpandListener 使软键盘在View 展开或折叠时出现或消失。通过上面这两种方法,我得到了预期的结果。我在几个关于 SO 的问题上发现了这一点(尤其是在 Close/hide the Android Soft Keyboard 和 Showing the soft keyboard for SearchView on ActionBar 或 Forcing the Soft Keyboard open)。
只是为了理解,当我单独使用SHOW_IMPLICIT 或SHOW_FORCED 时,它对较低版本(如2.+)没有影响。 EditText 被聚焦,但键盘没有出现(所以,你猜这是一件坏事)。在最近的版本中(例如 4.+),这是一个很好的效果,没有问题。然后,我用上面的 openKeyboard 方法强制键盘显示出来。
现在,我遇到了一些麻烦......
在较低版本上,我在创建/销毁键盘之前和之后都有“空白”空间,我可以忍受这个。 但是在最近的版本中,当我返回第一个活动时,我得到了“空白”空间。它在不到一秒钟的时间内就在这里,但足以看到!
为了更好地理解发生了什么,请参见下图:
1. 第二个活动:我按向上主页按钮 - 键盘正常消失。
2.(返回)第一个活动:我的 ListView 被一个“空白”空间(我的应用程序中的背景颜色)覆盖。然后它消失了(这与软键盘的高度相同,毫无疑问!)
我猜是因为我在第一个活动中强制键盘出现虽然我在第二个活动中也强制键盘隐藏,但是我该如何解决返回第一个活动时的“空白”空间?
总结
1) A 活动 => 按菜单中的项目 > 视图折叠 > 显示键盘 > 点击文本 > 发送 > 隐藏键盘 > 启动 B 活动。
2) B 活动 => 操作栏中的 setCustomView > 仅当编辑文本被聚焦/单击时才显示键盘 > 点击文本 > 发送 > 隐藏键盘 > 刷新内容 > 按主页按钮 > 返回 A 活动
3)一个活动=>“空”屏幕>屏幕消失。
任何帮助将不胜感激。
谢谢你的时间。
编辑
我添加了我第一堂课的代码,看看是否有人告诉我我做错了什么。也许是我的代码造成了问题。
菜单(操作视图)
ActionBar actionBar;
MenuItem itemSearchAction;
EditText mSearchEdit;
InputMethodManager mImm;
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
getSupportMenuInflater().inflate(R.menu.main, menu);
itemSearchAction = menu.findItem(R.id.action_search);
View v = (View) itemSearchAction.getActionView();
mSearchEdit = (EditText) v.findViewById(R.id.SearchEdit);
itemSearchAction.setOnActionExpandListener(this);
return true;
}
OnActionExpandListener
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
actionBar.setIcon(R.drawable.ic_app_search); // change icon
mSearchEdit.requestFocus(); // set focus on edittext
openKeyboard(); // the method above
mSearchEdit.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
closeKeyboard(); // same method as above
// new Intent() to second activity
// perform with startActivity();
itemSearchAction.collapseActionView(); // collapse view
return true;
}
return false;
}
});
// add a clicklistener to re-open the keyboard on lower versions
mSearchEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openKeyboard();
}
});
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
actionBar.setIcon(R.drawable.ic_app_logo); // change icon again
if(!(mSearchEdit.getText().toString().equals("")))
mSearchEdit.setText(""); // reinitial the edittext
return true;
}
OnOptionsItemSelected
// I had this verification when I make new Intent() to
// a new activity, just in case (works like a charm)
if(itemSearchAction.isActionViewExpanded())
itemSearchAction.collapseActionView();
ActionView(项目+布局)
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_app_search"
android:title="@string/action_search"
android:showAsAction="ifRoom|collapseActionView"
android:actionLayout="@layout/search_actionview" />
<EditText
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/SearchEdit"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_gravity="right|bottom" android:gravity="left"
android:layout_marginBottom="6dip"
android:hint="@string/action_search"
android:textColor="@color/white"
android:textColorHint="@color/white"
android:singleLine="true"
android:cursorVisible="true"
android:inputType="text"
android:imeOptions="actionSearch|flagNoExtractUi"
android:imeActionLabel="@string/action_search"
android:focusable="true" android:focusableInTouchMode="true"
android:background="@drawable/bt_edit_searchview_focused" >
<requestFocus />
</EditText>
更新
我看到很多类似的问题,EditText 中的ActionBar 即使焦点已设置也不会使键盘出现。我又试了一次(即使我已经测试了几次):
/*
* NOT WORKING
* Sources: https://stackoverflow.com/questions/11011091/how-can-i-focus-on-a-collapsible-action-view-edittext-item-in-the-action-bar-wh
* https://stackoverflow.com/a/12903527/2668136
*/
int mode = WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE;
getWindow().setSoftInputMode(mode);
postDelayed() to show: .showSoftInput(mSearchEdit, InputMethodManager.SHOW_IMPLICIT); - 200ms (Not working on lower versions)
postDelayed() to hide: .hideSoftInputFromWindow(mSearchEdit.getWindowToken(), 0); - 200ms
new Runnable() on edittext => requestFocus() + showSoftInput(SHOW_IMPLICIT/FORCED/HIDE_NOT_ALWAYS/HIDE_IMPLICIT_ONLY)
在我看来,只有SHOW_FORCED|HIDE_IMPLICIT_ONLY 可以强制键盘在视图折叠时自动显示。在此之后,在所有版本中,我必须将 hideSoftInputFromWindow 设置为 0 以隐藏它。
但是即使按下编辑文本,这也会不显示键盘,所以我添加了一个 ClickListener强制键盘再次显示(仅在较低版本上发生)。
更新 2:
这显然很奇怪,当我尝试像我在许多 SO 答案(带/不带 ABS)中看到的那样制作一点 Thread 时,在较低版本中没有任何反应。
我尝试了另一种方式。我创建了新线程,以便在调用隐藏键盘的新意图之前有很短的时间。我有强制关闭的键盘,好的。然后我打开了新的活动,OK。但现在我回来时,这是值得的!当我回来时,“空”空间也在较低版本上。我这样做了:
// close the keyboard before intent
closeKeyboard();
// make the intent after 500 ms
Handler handler = new Handler();
Runnable runner = new Runnable() {
public void run() {
// new intent with startActivity()
}
};
handler.postDelayed(runner, 500);
// collapse the View
itemSearchAction.collapseActionView();
这让我头疼!我不明白为什么在我的情况下,上面的提示不起作用,而在其他答案中,当他们使用新线程显示/隐藏键盘时,这非常有效。
注意:我的测试是在 (模拟器:) GalaxyNexus、NexusS、NexusOne 和 (真实设备:) Samsung GalaxySL (2.3. 6) 和 Nexus4 (4.4)。
如果有人可以帮助我解决这种丑陋的情况。提前致谢。
【问题讨论】:
-
尝试在你的Activity A的
onCreate方法中添加getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);看看。 -
我在
onCreate加上这两种方法都用过,但没什么变化。回去的时候还是一片空白。 -
@Blo 你找到这个问题的答案了吗?
标签: android android-actionbar android-view android-softkeyboard