[英]Android EditText, soft keyboard show/hide event?
是否可以捕获为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只是显示并隐藏其窗口; 您从中获得的反馈是窗口管理器,如果您将窗口内容设置为调整大小模式,则会调整其窗口内容的大小。
我使用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>
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)
方法来处理show / hide事件。
许多Android开发人员喜欢根据是否显示虚拟键盘来改变布局。因此,对于解决方案,您可以看到Android:检测软键盘打开。它对我有用,我认为它也非常有用。
您可以通过覆盖活动的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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.