2010-08-25 43 views
20

继承人的困境: 我在一个选项卡(总共有3个选项卡,它们位于屏幕底部)内显示一个带有3个输入字段和2个按钮的屏幕。 2个按钮设置在屏幕的左下角,右侧的标签上方。当我点击一个输入字段时,标签和按钮都被推上了键盘之上。Android:我怎么知道软键盘是否显示?

我希望只将按钮向上推,并将标签原来位于底部。我正在考虑将选项卡的可见性设置为“通过”,一旦我确定软键盘正在显示,并且一旦软键盘消失,可见性就可见。

是否有某种软键盘的听众,或者可能是输入字段?编辑文本中可能使用OnFocusChangeListener的一些棘手问题?我如何确定键盘是否可见?

+0

检查了这一点:http://stackoverflow.com/questions/4745988/how-do-i-detect-if-software-keyboard-is-visible-on-android-device/31090451#31090451 适合我。 – 2015-06-27 15:41:54

+0

[如何检查Android中软件键盘的可见性?](http:// stackoverflow。com/questions/2150078/how-to-check-android-visibility-of-software-keyboard-in-android) – afathman 2016-02-08 17:02:12

回答

5

确定键盘是否显示是不可能的。

您可能希望将其全部禁用到清单中的windowSoftInputMode xml标记:http://developer.android.com/reference/android/R.attr.html#windowSoftInputMode。或者你可以看看如何删除焦点来隐藏键盘:Hide soft keyboard on activity without any keyboard operations

两者都不能完全解决您的问题。我记得阅读一篇博文强烈建议不要在屏幕底部使用制表符,而是出于UI清晰的原因。我建议你跟进一下。

+1

还不可能确定是否显示软键盘?自从......五年过去了...... – 2015-12-06 17:00:48

0

这可能有助于 http://developer.android.com/reference/android/inputmethodservice/InputMethodService.html

https://android.googlesource.com/platform/packages/inputmethods/LatinIME/+/master/java/src/com/android/inputmethod/latin

键盘代码是在这里,我通过它有点grepped不得不放弃,我没有看到任何广播发送,这将是任何使用,至少在这里。也许你可以去repo中找到较低级别的代码并找到一个有用的Intent被发送。第一个linke可能会告诉你它何时变得可见,但我不知道如何判断它何时变得不可见。

-5

在我的最后一个应用程序,我在显示软键盘的一些问题,然后我用下面的代码在我的清单文件:

<activity android:screenOrientation="portrait" android:configChanges="keyboardHidden|orientation" android:name=".rate.Calculator"/>

1

据我所知,你不能。

但是,您可以监视布局的大小更改,并且由于键盘显示是调整大小的主要原因,因此您可能可以假定显示或不显示键盘。

以下是用于监视布局大小更改的示例代码。只需使用此布局作为原始布局的父级,然后使用其侦听器。如果高度已经下降,则可以假定显示键盘,并且如果增加了,则可以假定它已关闭。

public class LayoutSizeChangedSensorFrameLayout extends FrameLayout { 
    public enum SizeChange { 
     HEIGHT_INCREASED, HEIGHT_DECREASED, WIDTH_INCREASED, WIDTH_DECREASED 
    } 

    public interface OnLayoutSizeChangedListener { 
     void onSizeChanged(EnumSet<SizeChange> direction); 
    } 

    private OnLayoutSizeChangedListener mLayoutSizeChangeListener; 

    public LayoutSizeChangedSensorFrameLayout(final Context context) { 
     super(context); 
    } 

    public LayoutSizeChangedSensorFrameLayout(final Context context, final AttributeSet attributeSet) { 
     super(context, attributeSet); 
    } 

    public LayoutSizeChangedSensorFrameLayout(final Context context, final AttributeSet attrs, final int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    protected void onSizeChanged(final int w, final int h, final int oldw, final int oldh) { 
     super.onSizeChanged(w, h, oldw, oldh); 
     if (mLayoutSizeChangeListener != null) { 
      final EnumSet<SizeChange> result = EnumSet.noneOf(SizeChange.class); 
      if (oldh > h) 
       result.add(SizeChange.HEIGHT_DECREASED); 
      else if (oldh < h) 
       result.add(SizeChange.HEIGHT_INCREASED); 
      if (oldw > w) 
       result.add(SizeChange.WIDTH_DECREASED); 
      else if (oldw < w) 
       result.add(SizeChange.WIDTH_INCREASED); 
      if (!result.isEmpty()) 
       mLayoutSizeChangeListener.onSizeChanged(result); 
     } 
    } 

    public void setOnLayoutSizeChangedListener(final OnLayoutSizeChangedListener layoutSizeChangeListener) { 
     this.mLayoutSizeChangeListener = layoutSizeChangeListener; 
    } 

    public OnLayoutSizeChangedListener getOnLayoutSizeChangeListener() { 
     return mLayoutSizeChangeListener; 
    } 
} 
相关问题