如何捕获软键盘显示或隐藏的事件以供EditText使用?
如何捕获软键盘显示或隐藏的事件以供EditText使用?
嗨,我使用了以下解决方法:
由于我的内容视图是LinearLayout的子类(也可以是任何其他视图或视图组),我重写了onMeasure方法,如下所示:
@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);
}
以下方法可以帮助我在键盘显示时隐藏一些控件,否则恢复显示。
希望对你有所帮助。
Activity
的方法,而是 View
的方法。创建您自己的“View”,并覆盖此方法。 - Dmitry Zaytsev实际上并不存在需要捕获的事件。输入法只是在显示和隐藏其窗口;由此产生的反馈是窗口管理器会使你自己的窗口内容重新调整大小,如果你处于调整大小模式下。
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的根视图。
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();
}
}
Example:
layout/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)
方法来处理显示/隐藏事件。showSoftInput
传递 ResultReceiver
时能够接收用户的隐藏事件(例如返回按钮),那就太好了,但实际上并非如此。 - TWiStErRobonConfigurationChanged
方法来捕获此内容:@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();
}
}