本文介绍了空的空间,当我返回到活动(软键盘强制)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个而ActionView 动作条菜单项(使用的 ActionBarSherlock 的),我能够显示<$​​ C $ C>的EditText 在其搜索字段。这是一个输入启动另一个活动 CustomView 的动作条,它显示了相同的布局(我不使用任何迫使 SoftKeyboard 来出现在该第二活动,是没有问题的在这里)。当我想使显示的软键盘/在前看不见时,自动在第一个活动的观点崩溃了,我用的:

openKeyboard法

  mImm.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);
 

closeKeyboard法

  mImm.hideSoftInputFromWindow(edittext.getWindowToken(),0);
 

我用的是 ActionExpandListener 使SoftKeyboard出现或消失时,查看展开或折叠。这两种方法上面,我有预期的结果。我发现这几个问题上的SO(尤其是关闭/隐藏Android的软键盘显示软键盘搜索查看动作条上或的)。

刚刚明白,当我用 SHOW_IMPLICIT SHOW_FORCED 独自一人,这是对较低版本没有任何影响(如2. +)。该EditText上的重点是,但键盘没有出现(这样,你想这是一件坏事)。在最近的版本(如4 +为例),这是一个很好的效果,并没有问题。于是,我不得不键盘现身与 openKeyboard方法的上面。

现在,我得到了一些麻烦与此...
在较低的版本中,我得到了空的空间之前,创造了键盘之后/销毁,我可以忍受这一点。 在最近的版本中,我得到了空的空间,它会显示在我回归的第一个活动。和它在这里在不到一秒钟,但足以看到!
为了更好地了解发生了什么,见下图:

1 第二个活动:我preSS向上主页按钮 - 键盘正常消失
2 (回)的第一项活动:我的ListView是(在我的应用程序背景颜色)覆盖的空白空间。和消失的(这是SoftKeyboard,没有可能的疑问的同一高度!)

我想这是因为我被迫出现在键盘在我的第一个活动的虽然我也迫使键盘隐藏当我去了第二,但我怎么能解决空的空间,当我回到第一个活动呢?


摘要
1)活动=> $的P菜单$ PSS项目>视图崩溃>显示键盘>点击短信>发送>隐藏键盘>推出B活性。
2)B活性=> setCustomView在动作条>只显示如果的EditText集中在键盘/点击>点击短信>发送>隐藏键盘>刷新内容> preSS home键>回归A活动
3)活动=>空屏幕>屏幕上消失。

任何帮助将是非常美联社preciate。
谢谢你的时间。


修改
添加我的第一堂课我的code,看是否有人告诉我什么,我做错了。也许这是我的code,这使得该问题。

 动作条动作条;
菜单项itemSearchAction;
的EditText mSearchEdit;
InputMethodManager mImm;

@覆盖
公共布尔onCreateOptionsMenu(最后菜单菜单){
    。getSupportMenuInflater()膨胀(R.menu.main,菜单);

    itemSearchAction = menu.findItem(R.id.action_search);
    视图V =(查看)itemSearchAction.getActionView();
    mSearchEdit =(EditText上)v.findViewById(R.id.SearchEdit);
    itemSearchAction.setOnActionExpandListener(本);

    返回true;
}
 
  @覆盖
公共布尔onMenuItemActionExpand(菜单项项){
    actionBar.setIcon(R.drawable.ic_app_search); //更改图标
    mSearchEdit.requestFocus(); //设置焦点的EditText
    openKeyboard(); //上述方法
    mSearchEdit.setOnEditorActionListener(新OnEditorActionListener(){
        @覆盖
        公共布尔onEditorAction(TextView的V,INT actionId,KeyEvent的事件){
            如果(actionId == EditorInfo.IME_ACTION_SEARCH ||(事件= NULL和放大器;!&安培; event.getKey code()== KeyEvent.KEY code_ENTER)){
                closeKeyboard(); //如上述相同的方法
                //新的意向()到第二项活动
                //与startActivity执行();
                itemSearchAction.collapseActionView(); //崩溃观点
                返回true;
            }
            返回false;
        }
    });
    //添加clicklistener到较低的版本重新打开键盘
    mSearchEdit.setOnClickListener(新View.OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
            openKeyboard();
        }
    });
    返回true;
}

@覆盖
公共布尔onMenuItemActionCollapse(菜单项项){
    actionBar.setIcon(R.drawable.ic_app_logo); //再次更改图标
    如果(!(mSearchEdit.getText()。的toString()。等于()))
                            mSearchEdit.setText(); // reinitial的的EditText
    返回true;
}
 
  //我有这个验证,当我作出新的意向()来
//一个新的活动,以防万一(就像一个魅力)
如果(itemSearchAction.isActionViewExpanded())
         itemSearchAction.collapseActionView();
 
 &LT;项目
     机器人:ID =@ + ID / ACTION_SEARCH
     机器人:图标=@可绘制/ ic_app_search
     机器人:标题=@字符串/ ACTION_SEARCH
     机器人:showAsAction =ifRoom | collapseActionView
     机器人:actionLayout =@布局/ search_actionview/&GT;

&LT;的EditText
     的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
     机器人:ID =@ + ID / SearchEdit
     机器人:layout_width =FILL_PARENT机器人:layout_height =WRAP_CONTENT
     机器人:layout_gravity =右|底安卓重力=左
     机器人:layout_marginBottom =6dip
     机器人:提示=@字符串/ ACTION_SEARCH
     机器人:文字颜色=@色/白
     机器人:textColorHint =@色/白
     机器人:单线=真
     机器人:cursorVisible =真
     机器人:inputType =文本
     机器人:imeOptions =actionSearch | flagNoExtractUi
     机器人:imeActionLabel =@字符串/ ACTION_SEARCH
     机器人:可聚焦=真正的机器人:focusableInTouchMode =真
     机器人:背景=@可绘制/ bt_edit_searchview_focused&GT;

     &LT;不是requestFocus /&GT;
&LT; /的EditText&GT;
 


更新

我看到很多类似的问题,与的EditText 动作条这不是使键盘看起来更关注的焦点先后成立。我想这一次(即使我已经测试了几个时间):

  / *
*不工作
*资料来源: http://stackoverflow.com/questions/11011091/how-can-i-focus-on-a-collapsible-action-view-edittext-item-in-the-action-bar-wh
* http://stackoverflow.com/a/12903527/2668136
* /
INT模式= WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE;
getWindow()setSoftInputMode(模式)。

postDelayed()显示:.showSoftInput(mSearchEdit,InputMethodManager.SHOW_IMPLICIT); -  200毫秒(不工作在较低的版本)
postDelayed()来隐藏:.hideSoftInputFromWindow(mSearchEdit.getWindowToken(),0); -  200毫秒

新的Runnable()上的EditText =&GT;不是requestFocus()+ showSoftInput(SHOW_IMPLICIT /强制/ HIDE_NOT_ALWAYS / HIDE_IMPLICIT_ONLY)
 

这似乎与我,只有 SHOW_FORCED | HIDE_IMPLICIT_ONLY 可以强制键盘显示视图崩溃时自动。在此之后,在所有版本,我必须做一个 hideSoftInputFromWindow 0隐藏它。
这undisplays即使EditText上为pressed键盘,所以我增加了一个 ClickListener 强制键盘再次显示(这种情况只有在较低的版本)。


UPDATE2:
这显然​​是奇怪的,当我尝试做一点就像我在许多看到那么答案(有/无ABS),什么都不会发生在更低的版本。
我尝试了不同的方式。我创建了新的线程有一个很短的时间才致电隐藏的新意图键盘。我有它被迫关闭,OK键盘。然后我打开了新的活动,确定。但现在,当我回来,这是值得!而空的空间也较低版本时,我回来了。我这样做:

  //意图之前关闭键盘
closeKeyboard();
//使后500毫秒的意图
处理程序处理程序=新的处理程序();
可运行亚军=新的Runnable(){
    公共无效的run(){
        //新的意向与startActivity()
    }
};
handler.postDelayed(亚军,500);
//折叠视图
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);
 

I have an ActionView with menu item on ActionBar (using ActionBarSherlock), I'm able to display an EditText as a search field in it. It's an input to launch another Activity with a CustomView in ActionBar which it displays the same layout (I don't use anything to force the SoftKeyboard to appear in this second activity, there is no problem here). When I want to make the Soft Keyboard appears/disapears automatically when the view collapse in first activity, I use:

openKeyboard method

mImm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

closeKeyboard method

mImm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);

I use the ActionExpandListener to make the SoftKeyboard appears or disappears when the View expands or collapse. With these two methods above, I have the expected result. I found this on several questions on SO (especially on Close/hide the Android Soft Keyboard and Showing the soft keyboard for SearchView on ActionBar or Forcing the Soft Keyboard open).

Just to understand, when I used SHOW_IMPLICIT or SHOW_FORCED alone, it was no effect on lower versions (as 2.+). The EditText was focused but the keyboard didn't show up (so, you guess it was a bad thing). In recent versions (as 4.+ for example), it was a nice effect and no problem. Then, I forced keyboard to show up with the openKeyboard method above.

Now, I got some troubles with this...
On lower versions, I got "empty" space before and after the keyboard created/destroyed, I can live with this. BUT in recent versions, I got "empty" space which it displays when I return to the first Activity. And it's here during less than one second, but sufficient to see that!
To better understand what happens, see the image below:

1. Second Activity: I press the Up Home Button - the keyboard disappears properly.
2. (back to) First Activity: my ListView is covered by a "empty" space (background color in my application). And it disappears (this is the same height of the SoftKeyboard, no possible doubt!)

I guess it's because I forced the keyboard to appear in my first activity although I also forced the keyboard to hide when I go the second, but how can I resolve the "empty" space when I return to the first activity?


Summary
1) A activity => press item in menu > view collapse > show the keyboard > tap text > send it > hide keyboard > launch B activity.
2) B activity => setCustomView in actionbar > show the keyboard only if the edittext is focused/clicked > tap text > send it > hide keyboard > refresh content > press home button > return to A activity
3) A activity => "empty" screen > screen disappears.

Any help will be very appreciate.
Thanks for your time.


EDIT
I add my code of my first class, to see if someone tells me what I'm doing wrong. Maybe it's my code which makes the issue.

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>


UPDATE

I see a lot of similar issues, with EditText in ActionBar which not makes the keyboard appear even the focus has set. I tried this again (even if I already tested several time):

/*
* NOT WORKING
* Sources: http://stackoverflow.com/questions/11011091/how-can-i-focus-on-a-collapsible-action-view-edittext-item-in-the-action-bar-wh
* http://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)

It seems with me, only SHOW_FORCED|HIDE_IMPLICIT_ONLY can force the keyboard to show automatically when the view collapse. After this, in all versions, I must to make a hideSoftInputFromWindow to 0 for hiding it.
BUT this undisplays the keyboard even if the edittext is pressed, so I added an ClickListener to force the keyboard to show again (this happens only on lower versions).


UPDATE2:
It's clearly weird, when I try to make a little Thread like I saw in many SO answers (with/without ABS), nothing happens in lower versions.
I tried a different way. I created the new thread to have a short time before call the new intent for hide the keyboard. I had the keyboard which forced to close, OK. And then I opened the new activity, OK. But now when I return, it's worth! The "empty" space is also on lower versions when I come back. I did this:

// 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();

It gives me headaches! I don't understand why in my case, the tip above not working whereas on other answers, when they use a new thread to show/hide the keyboard, this works perfectly.

NOTE: my tests were on (emulator:) GalaxyNexus, NexusS, NexusOne and (real devices:) Samsung GalaxySL (2.3.6) and Nexus4 (4.4).

If someone can help me with this ugly situation. Thanks in advance.

解决方案

Did you tried this one?

setContentView(R.layout.activity_direction_3);
    getWindow().getDecorView().setBackgroundColor(
            android.R.color.transparent);

这篇关于空的空间,当我返回到活动(软键盘强制)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 04:03