保持键盘的开/关状态的EditText,当应用程序涉及到前台 [英] Maintain keyboard open/closed state for EditText when app comes to foreground

查看:106
本文介绍了保持键盘的开/关状态的EditText,当应用程序涉及到前台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动这是重新创建一个配置变化(期望)。我有一个 DialogFragment 这就要求 setRetainInstance(真)有一个的EditText 在其布局。

在DialogFragment的 onActivityCreated 我所说的:

<$p$p><$c$c>getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

A)如果我打开键盘,然后当我把程序放到后台,然后把它带到foregournd然后点击我想键盘仍会显示

B)如果我关闭键盘(EditText上仍具有焦点,并显示光标这是期望的行为),然后点击我想键盘仍然关闭如果我把程序放到后台,然后将它的前景。

我似乎无法达到既A)和B)。当我把应用推到前台的键盘总是关闭。我试过.SOFT_INPUT_STATE_ALWAYS_VISIBLE但随后的键盘始终是敞开的。

先谢谢您的任何建议,我会如何实现这一目标。我还要保持整个转动,键盘状态,但我要离开,对于另一天。彼得。

修改 请注意,我做的不可以要prevent被在配置更改重新创建的活动。

我还尝试了 WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED 它做了一个电话(单面板布局)在旋转过程中保持键盘的开/关状态,但一)没有工作用双面板布局二)使应用推到前台时,没有保持键盘状态。

解决方案

您好:首先感谢一个有趣的问题。这让我尝试code。在这里,我描述我的解决方案

要找到解决方案,我必须知道两件事情

1 如何检测softkeyboard是否可见或不可见。

2 如何设置softkeyboard可见还是隐藏。

我在下面的步骤解决方案 搜索了一下后,我意识到,检测softkeyboard状态的最佳解决方案(可见/隐藏)是使用<一个href="http://developer.android.com/reference/android/view/ViewTreeObserver.html">ViewTreeObserver.我直接指向这样回答知道,如果你不知道。这里是链接

和设置我只是用softkeyboardstate Window.setSoftInputMode 方法。

和了解用户交互我重写 onUserInteraction 方法

养了两个标志。一个的标志是preserve keyboarstate另一种是知道应用程序是否去背景或没有

code:

1。变量声明

  INT lastDiff = 0;
挥发性布尔标志= FALSE;
挥发性INT FLAG2 = 0;
 

2。 ViewTreeObserver

  activityRootView.getViewTreeObserver()。addOnGlobalLayoutListener(
            新OnGlobalLayoutListener(){
                @覆盖
                公共无效onGlobalLayout(){
                    矩形R =新的矩形();
                    activityRootView.getWindowVisibleDisplayFrame(r)的;

                    INT heightDiff = activityRootView.getRootView()
                            .getHeight() - (r.bottom  -  r.top);
                    如果(lastDiff == heightDiff)
                        返回;
                    lastDiff = heightDiff;
                    Log.i(aerfin,arefin+ lastDiff);
                    如果(heightDiff→100){//如果多于100个像素,其
                                            //可能是键盘...
                        FLAG2 = 0;
                    } 其他 {
                        如果(旗==假)
                            FLAG2 = 1;
                    }
                }
            });
 

3。处理用户交互

  @覆盖
 公共无效onUserInteraction(){
 // TODO自动生成方法存根
 super.onUserInteraction();
 标志=真正的;
 }
 

4。最后的onPause和onresume

  @覆盖
保护无效的onPause(){
    // TODO自动生成方法存根
    super.onPause();
    标志=真正的;
}

@覆盖
保护无效onResume(){
    标志= FALSE;

    // TODO自动生成方法存根
    开关(FLAG2){
    情况下0:

        getWindow()。setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        打破;
    情况1:
        getWindow()。setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

        打破;
    默认:
        打破;
    }

    super.onResume();
}
 

说明:

在这里,我用了两个标志(FLAG2和标志)。 FLAG2 preserves的keyboardstate和标志preserves申请是否被切换到后台或有任何用户交互。标志使用,因为当应用程序被切换到后台,然后在第一,它隐藏键盘。其他的事情可以从code以上是很容易理解。

测试:

在S2(ICS),Desire S的(ICS),银河ÿ测试(2.3.6)

最后的评论:

我又写道的code快,因此可能会错过了一些其他的优化​​。也有可能是特殊情况的机会。如果屏幕改变为某些原因而键盘其他则可能不能够检测到键盘状态。

I have an Activity which is recreated on a config change (desired). I have a DialogFragment which calls setRetainInstance(true) with a single EditText in its layout.

In the DialogFragment's onActivityCreated I call:

getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

A) If I open the keyboard then when I put the app into the background and then bring it to the foregournd then I want the keyboard to still be displayed.

B) If I close the keyboard (EditText still has focus and shows cursor which is desired behaviour) then I want the keyboard to still be closed if I put the app into the background and then bring it to the foreground.

I can't seem to achieve both A) and B). The keyboard is always closed when I bring the app to the foreground. I've tried .SOFT_INPUT_STATE_ALWAYS_VISIBLE but then the keyboard is always open.

Thanks in advance for any suggestions as to how I might achieve this. I also wish to maintain such keyboard state across rotation but I'm leaving that for another day. Peter.

Edit Please note that I do not want to prevent the activity from being re-created on a configuration change.

I also experimented with WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED which did maintain the keyboard open/close state during rotation on a phone (single pane layout) but a) did not work with a dual pane layout b) did not maintain the keyboard state when bringing the app to the foreground.

解决方案

Hello first of all thanks for an interesting question. It made me experiment with code . Here I am describing my solution

To find the solution I had to know two things

1. how to detect whether a softkeyboard is visible or not

2. How to set softkeyboard visible or hidden.

I got the solution in the following steps after searching a bit I realized that the best solution of detecting a softkeyboard state (visible/hidden) is to use ViewTreeObserver. I am directly pointing to a so answer to know about it if you dont know. here is the link

and to set the softkeyboardstate I just used Window.setSoftInputMode method.

and to know a user interaction I override onUserInteraction method

Kept two flag. one flag is to preserve keyboarstate another is to know whether the application went to background or not

CODE:

1. variable declared

int lastDiff = 0;
volatile boolean flag = false;
volatile int flag2 = 0;

2. ViewTreeObserver

    activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(
            new OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    Rect r = new Rect();
                    activityRootView.getWindowVisibleDisplayFrame(r);

                    int heightDiff = activityRootView.getRootView()
                            .getHeight() - (r.bottom - r.top);
                    if (lastDiff == heightDiff)
                        return;
                    lastDiff = heightDiff;
                    Log.i("aerfin","arefin "+lastDiff);
                    if (heightDiff > 100) { // if more than 100 pixels, its
                                            // probably a keyboard...
                        flag2 = 0;
                    } else {
                        if (flag == false)
                            flag2 = 1;
                    }
                }
            });

3. Handling user interaction

 @Override
 public void onUserInteraction() {
 // TODO Auto-generated method stub
 super.onUserInteraction();
 flag = true;
 }

4. finally onpause and onresume

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    flag = true;
}

@Override
protected void onResume() {
    flag = false;

    // TODO Auto-generated method stub
    switch (flag2) {
    case 0:

        getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        break;
    case 1:
        getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

        break;
    default:
        break;
    }

    super.onResume();
}

Explanation:

Here I used two flags (flag2 and flag). flag2 preserves the keyboardstate and flag preserves whether the application goes to background or is there any user interaction. flag is used because when application goes to background then at first it hides the keyboard. Other things can be easily understood from the code above.

Test:

tested in s2(ics), desire s (ics), galaxy y (2.3.6)

Final comment:

I worte the code quickly so might be missed some other optimization. ALso there might be chance of exceptional cases. If the screen changes for some reasons other than keyboard then it might not be able to detect keyboard state.

这篇关于保持键盘的开/关状态的EditText,当应用程序涉及到前台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆