是否可以捕捉到为 EditText 显示或隐藏软键盘的事件?
7 回答
嗨,我使用了以下解决方法:
至于我的内容视图是 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);
}
此解决方法帮助我在键盘显示时隐藏一些控件,否则将其带回来。
希望这会很有用。
实际上没有这样的事件可以捕捉。IME 只是显示和隐藏它的窗口;您从中获得的反馈是窗口管理器,如果您将其置于调整大小模式,则会导致您自己的窗口内容调整大小。
我通过使用 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 的根视图。
就我而言,我想在显示软键盘时隐藏底栏。当布局的大小小于正常布局大小的百分比时,我认为最好只隐藏栏。因此,考虑到软键盘通常占用 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>
我的活动.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)
。您可以覆盖类的onReceiveResult(int resultCode, Bundle resultData)
方法ResultReceiver
来处理显示/隐藏事件。
许多 Android 开发人员喜欢根据是否显示虚拟键盘来更改布局。因此,对于解决方案,您可以看到Android: Detect softkeyboard open。它对我有用,我认为它也很有用。
您可以通过覆盖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();
}
}