调整布局时,软键盘上 [英] Adjust layout when soft keyboard is on

查看:93
本文介绍了调整布局时,软键盘上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在某些应用中布局的变化已经看到了在软键盘显示。这当然不是 adjustPan ,因为整个布局(可能是内部的布局)的转变,不仅是当前的EditText 。这是例如在Evernote的登录屏幕。你能指点如何做?

I've seen in some applications the layout shifts when soft keyboard is shown. This is certainly not adjustPan because the whole layout (probably inner layout) shifts, not only the current EditText. This is for instance in Evernote login screen. Can you advice how this made?

推荐答案

下面是一个解决方案,它的工作原理类似Evernote的登录屏幕:

Here's a solution that works like the Evernote login screen:

首先,定义一个类,将成为你的特殊的LinearLayout是这样的:

First, define a class that will be your special LinearLayout like this:

public class MyLayout extends LinearLayout {

public MyLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public MyLayout(Context context) {
    super(context);
}

private OnSoftKeyboardListener onSoftKeyboardListener;

@Override
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
    if (onSoftKeyboardListener != null) {
        final int newSpec = MeasureSpec.getSize(heightMeasureSpec); 
        final int oldSpec = getMeasuredHeight();
        if (oldSpec > newSpec){
            onSoftKeyboardListener.onShown();
        } else {
            onSoftKeyboardListener.onHidden();
        }
    }
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

public final void setOnSoftKeyboardListener(final OnSoftKeyboardListener listener) {
    this.onSoftKeyboardListener = listener;
}

public interface OnSoftKeyboardListener {
    public void onShown();
    public void onHidden();
}

}

此布局听来测算,如果新的测量<比旧的,这意味着在屏幕的部分由软键盘吃掉。

This layout listens to measure changes, and if new measurements are < than the old ones, that means part of the screen is eaten by soft keyboard.

虽然,它的工作,在你的清单,你需要设置的android:windowSoftInputMode =adjustResize这样的内容将被调整,而不仅仅是移动

Though, for it to work, in your manifest you need to set android:windowSoftInputMode="adjustResize" so the content will be resized and not just shifted.

和整个系统的工作原理如下: 你有你的布局:

And the whole system works as follows: You have your layout:

<MyLayout id="layout">
  <SomeImage id="image"/>
  <SomeText>
  <SomeInput>
</MyLayout>

这就像evernotes登录屏幕。 然后,在你的活动:

It's like evernotes login screen. Then, in your activity:

((MyLayout)findViewById(R.id.layout)).setOnSoftKeyboardListener(new OnSoftKeyboardListener() {
        @Override
        public void onShown() {
            findViewById(R.id.image).setVisibility(View.GONE);
        }
        @Override
        public void onHidden() {
            findViewById(R.id.image).setVisibility(View.VISIBLE);
        }
    });

然后去的manifest.xml和设置

Then go to manifest.xml and set

android:windowSoftInputMode="adjustResize"

会发生什么,如果是软键盘显示,它会隐藏图像和将调整内容的其余部分。 (实际上,你可以看到文本大小在Evernote的)

What will happen, is when soft keyboard is shown, it'll hide the image and will resize the rest of content. (You can actually see how text is resized in Evernote)

图像隐藏,当然,对很多事情可以做的。但是你一定要小心,因为不同布局的变化也将调用onMeasure。

Image hide is, of course, one of the many things you can do. But you must be careful, since different layout changes will also call onMeasure.

当然,这是一个肮脏的变种。您需要检查的方向变化,正确的时间比较新规范与旧的时候,当采取实际测量,也许一些更多的逻辑。但我认为这是做的唯一途径。

Of course it's a dirty variant. You need to check for orientation changes, and the right time when actually take the measurements, and maybe some more logic when comparing the new specs with the old ones. But i think this is the only way to do it.

这篇关于调整布局时,软键盘上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆