【问题标题】:how to scroll up layout when keyboard visible in activity with FULL_SCREEN当键盘在 FULL_SCREEN 的活动中可见时如何向上滚动布局
【发布时间】:2013-05-15 09:13:44
【问题描述】:

当软输入键盘可见时,我想向上滚动布局。我在 xml 中的适当位置定义了滚动视图,但是当键盘可见时,它会隐藏布局的某些部分,例如按钮。 我在 stackoveflow Link 上读到,当活动为 FULL_SCREEN 时,滚动视图不起作用。如果是这样,那么当软输入可见时如何向上滚动布局。

【问题讨论】:

标签: android scrollview android-fullscreen soft-input-panel


【解决方案1】:

使用此自定义相对布局来检测您的 xml 中的软键盘

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

 /**
 * RelativeLayout that can detect when the soft keyboard is shown and hidden.
 *  
 */

public class RelativeLayoutThatDetectsSoftKeyboard extends RelativeLayout {

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

public interface Listener {
    public void onSoftKeyboardShown(boolean isShowing);
}
private Listener listener;
public void setListener(Listener listener) {
    this.listener = listener;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int height = MeasureSpec.getSize(heightMeasureSpec);
    Activity activity = (Activity)getContext();
    Rect rect = new Rect();
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
    int statusBarHeight = rect.top;
    int screenHeight = activity.getWindowManager().getDefaultDisplay().getHeight();
    int diff = (screenHeight - statusBarHeight) - height;
    if (listener != null) {
        listener.onSoftKeyboardShown(diff>128); // assume all soft keyboards are at least 128 pixels high
    }
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);       
}

}

然后为您的活动类实现RelativeLayoutThatDetectsSoftKeyboard.Listener

   RelativeLayoutThatDetectsSoftKeyboard mainLayout =  (RelativeLayoutThatDetectsSoftKeyboard)V.findViewById(R.id.dealerSearchView);
   mainLayout.setListener(this);




    @Override
public void onSoftKeyboardShown(boolean isShowing) {
    if(isShowing) {


    } else {

    }
}

基于键盘的可见性,使用布局参数上下移动布局

【讨论】:

    【解决方案2】:

    您必须更改清单文件

    在您的活动标签中

    android:windowSoftInputMode="adjustPan" 添加这个。

    看到这个http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft

    【讨论】:

    • 我已经定义了 android:windowSoftInputMode="stateVisible|adjustResize|adjustPan" 但仍然无法正常工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-25
    • 2019-04-29
    • 1970-01-01
    • 2018-06-11
    • 2012-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多