Android EditText,软键盘显示/隐藏事件?

是否有可能赶上软键盘显示或隐藏的EditText事件?

嗨我使用以下解决方法:

至于我的内容视图是LinearLayout的子类(可以是任何其他视图或视图组),我会重写onMeasure方法lilke下面:

@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { final int proposedheight = MeasureSpec.getSize(heightMeasureSpec); final int actualHeight = getHeight(); if (actualHeight > proposedheight){ // Keyboard is shown } else { // Keyboard is hidden } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } 

这种解决方法帮助我隐藏了一些控件,当键盘显示并带回来,否则。

希望这将是有益的。

实际上并没有这样的事件发生。 IME只是简单地显示和隐藏它的窗口; 你从这里得到的反馈是窗口pipe理器,如果你把它放在resize的模式,导致你自己的窗口内容的大小。

我通过使用onGlobalLayoutListener解决了这个问题:

  final View activityRootView = findViewById(R.id.top_root); activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { public void onGlobalLayout() { int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight(); if (heightDiff > 100) { // keyboard is up } else { // keyboard is down } } }); 

这里activityRootView是你Activity的根视图。

在我的情况下,我想要显示softkeyboard时隐藏底部栏。 当布局的尺寸小于正常布局尺寸的百分比时,我认为最好隐藏栏。 所以我使用这个工作正常的解决scheme,考虑到软键盘通常需要20%或更多的屏幕高度。 只要改变百分比常数,你可能认为没问题。 它需要清单中的属性android:windowSoftInputMode =“adjustResize” ,布局必须是工作的根。

从任何布局,而不是RelativeLayout扩展。

 public class SoftKeyboardLsnedRelativeLayout extends RelativeLayout { private boolean isKeyboardShown = false; private List<SoftKeyboardLsner> lsners=new ArrayList<SoftKeyboardLsner>(); private float layoutMaxH = 0f; // max measured height is considered layout normal size private static final float DETECT_ON_SIZE_PERCENT = 0.8f; public SoftKeyboardLsnedRelativeLayout(Context context) { super(context); } public SoftKeyboardLsnedRelativeLayout(Context context, AttributeSet attrs) { super(context, attrs); } @SuppressLint("NewApi") public SoftKeyboardLsnedRelativeLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { final int newH = MeasureSpec.getSize(heightMeasureSpec); if (newH > layoutMaxH) { layoutMaxH = newH; } if (layoutMaxH != 0f) { final float sizePercent = newH / layoutMaxH; if (!isKeyboardShown && sizePercent <= DETECT_ON_SIZE_PERCENT) { isKeyboardShown = true; for (final SoftKeyboardLsner lsner : lsners) { lsner.onSoftKeyboardShow(); } } else if (isKeyboardShown && sizePercent > DETECT_ON_SIZE_PERCENT) { isKeyboardShown = false; for (final SoftKeyboardLsner lsner : lsners) { lsner.onSoftKeyboardHide(); } } } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } public void addSoftKeyboardLsner(SoftKeyboardLsner lsner) { lsners.add(lsner); } public void removeSoftKeyboardLsner(SoftKeyboardLsner lsner) { lsners.remove(lsner); } // Callback public interface SoftKeyboardLsner { public void onSoftKeyboardShow(); public void onSoftKeyboardHide(); } } 

例:

布局/ my_layout.xml

 <?xml version="1.0" encoding="utf-8"?> <yourclasspackage.SoftKeyboardLsnedRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/myLayout" android:layout_width="match_parent" android:layout_height="match_parent"> ... </yourclasspackage.SoftKeyboardLsnedRelativeLayout> 

MyActivity.java

 public class MyActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_layout); SoftKeyboardLsnedRelativeLayout layout = (SoftKeyboardLsnedRelativeLayout) findViewById(R.id.myLayout); layout.addSoftKeyboardLsner(new SoftKeyboardLsner() { @Override public void onSoftKeyboardShow() { Log.d("SoftKeyboard", "Soft keyboard shown"); } @Override public void onSoftKeyboardHide() { Log.d("SoftKeyboard", "Soft keyboard hidden"); } }); } } 

试试这些方法: showSoftInput(View, int, ResultReceiver)hideSoftInputFromWindow(IBinder, int, ResultReceiver) 。 您可以重写ResultReceiver类的onReceiveResult(int resultCode, Bundle resultData)方法来处理显示/隐藏事件。

许多Android开发人员喜欢根据是否显示虚拟键盘来改变布局。因此,对于解决scheme,您可以看到Android:检测软键盘打开 ,它正在为我工​​作,我认为它也是如此有用。

你可以通过覆盖你的activity的onConfigurationChanged方法来捕获这个方法:

 @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); if(newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) { ((SherlockFragmentActivity)getActivity()).getSupportActionBar().hide(); } else if(newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES){ ((SherlockFragmentActivity)getActivity()).getSupportActionBar().show(); } }