fll*_*llo 5 android android-softkeyboard android-view android-actionbar
我有一个ActionView带菜单项ActionBar(使用ActionBarSherlock),我可以在其中显示EditText一个搜索字段.这是一个Activity用CustomViewActionBar 启动另一个的输入,它显示相同的布局(我没有使用任何东西来强制它SoftKeyboard出现在第二个活动中,这里没有问题).当我想让第一个活动中的视图崩溃时软键盘自动显示/消失,我使用:
openKeyboard方法
mImm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
Run Code Online (Sandbox Code Playgroud)
closeKeyboard方法
mImm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);
Run Code Online (Sandbox Code Playgroud)
我使用它ActionExpandListener来使SoftKeyboard在View展开或折叠时出现或消失.通过以上两种方法,我得到了预期的结果.我在几个关于SO的问题上发现了这一点(特别是关闭/隐藏Android软键盘并在ActionBar上显示SearchView的软键盘或强制打开软键盘).
只是为了理解,当我使用SHOW_IMPLICIT或SHOW_FORCED单独使用时,它对较低版本没有影响(如2. +).EditText是专注的,但键盘没有出现(所以,你猜它是一件坏事).在最近的版本中(例如4. +),这是一个很好的效果,没有问题.然后,我强制键盘显示上面的openKeyboard方法.
现在,我遇到了一些麻烦...
在较低的版本中,我在键盘创建/销毁之前和之后得到了"空"空间,我可以忍受这个.但是在最近的版本中,当我返回第一个Activity时,我得到了"空"空间.而且它在不到一秒的时间就在这里,但足以看到它!
为了更好地了解会发生什么,请参见下图:
1.第二项活动:我按下向上主页按钮 - 键盘正常消失.
2.(返回)第一个活动:我的ListView被一个"空"空间覆盖(我的应用程序中的背景颜色).它消失了(这与SoftKeyboard的高度相同,毫无疑问!)
我想这是因为我强迫键盘出现在我的第一个活动中,虽然我在第二次活动时也强迫键盘隐藏,但是当我返回第一个活动时如何解决"空"空间?
摘要
1)活动=>按菜单中的项目>查看折叠>显示键盘>点击文本>发送>隐藏键盘>启动B活动.
2)B activity => setCustomView in actionbar>仅在编辑文本被聚焦/点击时显示键盘>点击文本>发送>隐藏键盘>刷新内容>按主页按钮>返回活动
3)活动=>"空"屏幕>屏幕消失.
任何帮助将非常感激.
谢谢你的时间.
编辑
我添加了我的第一堂课的代码,看看是否有人告诉我我做错了什么.也许这是我的代码问题.
菜单(ActionView)
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;
}
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
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();
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
UPDATE
我看到很多类似的问题,与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)
Run Code Online (Sandbox Code Playgroud)
在我看来,只有SHOW_FORCED|HIDE_IMPLICIT_ONLY当视图崩溃时才能强制键盘自动显示.在此之后,在所有版本中,我必须使用hideSoftInputFromWindow0来隐藏它.
但即使按下edittext,这也会显示键盘,因此我添加了一个ClickListener强制键盘再次显示(这只在较低版本上发生).
更新2:
这显然很奇怪,当我尝试做一些Thread像我在许多SO答案中看到的那样(有/没有ABS)时,在较低版本中没有任何反应.
我尝试了另一种方式.我创建了新线程,以便在调用隐藏键盘的新意图之前有一段时间.我的键盘被迫关闭,好的.然后我打开了新活动,好的.但现在当我回来时,这是值得的!当我回来时,"空"空间也在较低版本.我这样做了:
// 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();
Run Code Online (Sandbox Code Playgroud)
它让我很头疼!我不明白为什么在我的情况下,上面的提示没有工作,而在其他答案,当他们使用新的线程来显示/隐藏键盘时,这完美地工作.
注意:我的测试是在(模拟器:) GalaxyNexus,NexusS,NexusOne和(真实设备:)三星GalaxySL(2.3.6)和Nexus4(4.4).
如果有人可以帮我解决这个丑陋的情况.提前致谢.
你试过这个吗?
setContentView(R.layout.activity_direction_3);
getWindow().getDecorView().setBackgroundColor(
android.R.color.transparent);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1960 次 |
| 最近记录: |