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

时间:2010-09-25 08:44:44

标签: android keyboard-events android-softkeyboard

是否可以捕获为EditText显示或隐藏软键盘的事件?

7 个答案:

答案 0 :(得分:29)

您好我使用了以下解决方法:

至于我的内容视图是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);
}

此解决方法帮助我在键盘显示时隐藏了一些控件,否则将其带回。

希望这会有用。

答案 1 :(得分:18)

实际上没有抓住这样的事件。 IME只是显示并隐藏其窗口;您从中获得的反馈是窗口管理器,如果您将窗口内容设置为调整大小模式,则会调整其窗口内容的大小。

答案 2 :(得分:4)

我使用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的根视图。

答案 3 :(得分:2)

在我的情况下,我想在显示软键盘时隐藏底栏。当布局小于正常布局大小的百分比时,我认为最好只隐藏栏。所以我使用这个解决方案,因为软键盘通常需要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");
            }
        });
    }
}

答案 4 :(得分:1)

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

答案 5 :(得分:0)

许多Android开发人员喜欢根据是否显示虚拟键盘来改变布局。因此,对于解决方案,您可以看到Android: Detect softkeyboard open。它对我有用,我认为它也非常有用

答案 6 :(得分:-6)

您可以通过覆盖活动的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();
   }
}