我有一个带有ActionView
菜单项的ActionBar
(使用ActionBarSherlock),我可以在其中显示EditText
作为搜索字段。这是在ActionBar中启动另一个带有Activity
的CustomView
的输入,它显示相同的布局(我不使用任何强制SoftKeyboard
出现在第二个 Activity 中的方法,这里没有问题)。当我希望在第一次 Activity 中 View 折叠时使软键盘自动显示/消失时,我使用:
openKeyboard方法
mImm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
close键盘方法
mImm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);
当
ActionExpandListener
展开或折叠时,我使用View
使SoftKeyboard出现或消失。通过以上两种方法,我得到了预期的结果。我在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方法中。现在,我遇到了一些麻烦...
在较低的版本上,在键盘创建/销毁之前和之后,我都有“空”的空间,我可以忍受这个空间。 但在最新版本中为,当我返回第一个Activity 时,它显示了“空”空间。它在这里不到一秒钟,但足以看到它!
为了更好地了解会发生什么,请参见下图:
1. 第二 Activity :我按了“向上主页”按钮-键盘正确消失了。
2. (返回)第一个 Activity :我的ListView被“空”空间(应用程序中的背景色)覆盖。它消失了(这与SoftKeyboard的高度相同,毫无疑问!)
我猜想是因为我强制键盘在我的第一个 Activity 中出现,尽管我在第二次 Activity 时也强制键盘隐藏了,但是当我返回第一个 Activity 时如何解决“空”空间?
概括
1)A Activity =>按下菜单中的项目> View 折叠>显示键盘>点击文本>发送它>隐藏键盘>启动B Activity 。
2)B Activity => Action 栏中的setCustomView>仅在焦点/单击编辑文本时才显示键盘>点击文本>发送>隐藏键盘>刷新内容>按主页按钮>返回A Activity
3) Activity =>“空”屏幕>屏幕消失。
任何帮助将不胜感激。
谢谢你的时间。
编辑
我添加了我第一个类的代码,看是否有人告诉我我做错了什么。也许是我的代码引起了问题。
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;
}
@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;
}
// 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();
<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
才能在 View 折叠时强制键盘自动显示。此后,在所有版本中,我都必须将hideSoftInputFromWindow
设置为0才能隐藏它。但是即使在按下edittext的情况下也不会显示键盘,因此我添加了
ClickListener
强制键盘再次显示(仅在较低版本上发生)。UPDATE2:
显然,这很奇怪,当我尝试制作一些
Thread
时,就像我在许多SO答案中看到的那样(使用或不使用ABS),在较低版本中什么也没有发生。我尝试了另一种方式。我创建了新线程,以便在调用新 Intent 隐藏键盘之前有很短的时间。我的键盘被迫关闭,确定。然后我打开了新 Activity ,确定。但是现在,当我返回时,就值得了!当我回来时,“空”空间也在较低的版本上。我是这样做的:
// 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和(实际设备:)三星GalaxySL(2.3.6)和Nexus4(4.4)上进行的。
如果有人可以帮助我解决这个丑陋的情况。提前致谢。
最佳答案
你尝试过这个吗?
setContentView(R.layout.activity_direction_3);
getWindow().getDecorView().setBackgroundColor(
android.R.color.transparent);