如何在设置android:windowSoftInputMode =“adjustPan”时滚动我的布局?

6
我的活动有一个顶部栏和底部栏。在顶部栏和底部栏之间的空间中,我有一个线性布局,里面有几个编辑文本视图。因为我不想让我的布局每次软键盘弹出时都重新调整大小,所以我在清单文件中为我的活动设置了android:windowSoftInputMode="adjustPan"。但当软键盘打开时,我想向下滚动以选择另一个要输入的编辑文本,但它不允许我这样做。只有在关闭软键盘时,我才能选择底部的编辑文本。这非常令人烦恼和不便。 我该如何使滚动视图和调整pan模式的软键盘一起很好地工作?请帮帮我,非常感谢。

我有同样的问题,但我不喜欢像你那样编写“假代码”来解决它... 嗯 - Ted
到目前为止,这个解决方案运作良好,我也寻找了一个更简单的替代方案。希望其他人能分享一个更好的解决此问题的方法。 - MichaelP
但是这个解决方案会在软键盘弹出时隐藏按钮。但是,根据屏幕分辨率等因素,可能不需要隐藏按钮,对吧? - Ted
这个解决方案不会在软键盘弹出时调整您的视图大小,您可以滚动视图以查看被软键盘隐藏的控件。很抱歉我不太理解您的情况。 - MichaelP
2个回答

4
最后,我找到了解决我的问题的方法,所以我想分享给未来可能遇到相同问题的人。我的布局简要描述如下:
<myRelativeLayout>
<topbar.../>
<myscrollView>
    <linearLayout>
        //all stuff controls:editview,textview,....
    </linearLayout>
</myscrollView>
<bottombar.../>

我创建了一个自定义类,名为myRelativeLayout,它继承自RelativeLayout。
public class myRelativeLayout extends RelativeLayout{

public interface OnRelativeLayoutChangeListener {
    void onLayoutPushUp();
    void onLayoutPushDown();
}

private OnRelativeLayoutChangeListener layoutChangeListener;
public myRelativeLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    final int proposedheight = MeasureSpec.getSize(heightMeasureSpec);
    final int actualHeight = getHeight();

    if (actualHeight > proposedheight){
        // Keyboard is shown
        layoutChangeListener.onLayoutPushUp();
    } else if(actualHeight < proposedheight){
        // Keyboard is hidden
        layoutChangeListener.onLayoutPushDown();
    }       
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

public void setLayoutChangeListener(OnRelativeLayoutChangeListener layoutChangeListener) {
    this.layoutChangeListener = layoutChangeListener;
}

public OnRelativeLayoutChangeListener getLayoutChangeListener() {
    return layoutChangeListener;
}

在我的活动中,我只需为myRelativeLayout设置setLayoutChangeListener,以在软键盘弹出时隐藏bottombar,并在软键盘隐藏时显示bottombar:
myRlayout.setLayoutChangeListener(new OnRelativeLayoutChangeListener() {

        @Override
        public void onLayoutPushUp() {
            // TODO Auto-generated method stub
            myBottombar.setVisibility(View.GONE);//in my case i need to setVisibility(View.GONE) to bottombar in order for this bar is not displayed when softkeyboard show up.
        }

        @Override
        public void onLayoutPushDown() {
            // TODO Auto-generated method stub
            myBottombar.setVisibility(View.VISIBLE);// redisplay myBottombar when keyboard is closed.

        }
    });

希望您能记得在活动中设置android:windowSoftInputMode="adjustResize"。希望这对于遇到相同问题的人有所帮助。

谢谢您的回复,不过有没有一种方法可以在不使用“伪造”所需功能的额外编码的情况下完成它呢? - Ted

1
将这些EditText放在一个像这样的ScrollView中:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <requestFocus />
        </EditText>

        <EditText
            android:id="@+id/editText2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <EditText
            android:id="@+id/editText3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

</ScrollView>

我不是在问如何在XML中制作布局,而是想知道当我为我的活动设置android:windowSoftInputMode="adjustPan"时如何滚动我的布局。 - MichaelP

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接