2

我有一个垂直滚动视图,其中包含几个单行编辑文本。

我的布局只有横向。

我无法将滚动视图滚动到具有光标的编辑文本之外。

如果我更改其中包含光标的编辑文本,我可以滚动直到该编辑文本到达屏幕的顶部或底部边缘。

此活动上唯一可以接收光标/焦点的控件是滚动视图中的编辑文本。将它们设置为没有焦点可以解决滚动问题,但显然我不能输入任何值。

请告知我该如何解决这个问题?

由于这种布局的复杂性,我只列出了滚动视图部分:

     <LinearLayout android:id="@+id/LinearLayoutRight" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="37" android:orientation="vertical">
    <ScrollView android:id="@+id/scrollView1" android:layout_width="match_parent" android:layout_height="match_parent">
        <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
            <LinearLayout android:id="@+id/counter_pat_section_row1" android:layout_width="match_parent" android:layout_height="108dp" android:layout_marginTop="1dp" android:background="@drawable/shape" android:orientation="vertical">
                <LinearLayout android:id="@+id/LinearLayout05" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="4dp" android:layout_weight="1">
                    <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center" android:layout_marginLeft="4dp" android:layout_weight="5">
                        <TextView android:id="@+id/TextView01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginRight="2dp" android:gravity="right" android:text="Stop at row1" android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="@color/white1"/>
                    </LinearLayout>
                    <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center" android:layout_weight="3">
                        <EditText android:id="@+id/rowmax1" android:layout_width="fill_parent" android:layout_height="40dp" android:layout_gravity="center_vertical" android:layout_marginRight="3dp" android:layout_weight="1" android:inputType="number" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="@color/black"/>
                    </LinearLayout>
                    <LinearLayout android:id="@+id/LinearLayout1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center_vertical" android:layout_marginRight="3dp" android:layout_weight="2" android:gravity="center_vertical" android:orientation="vertical">
                        <Button android:id="@+id/resetrow" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:background="@drawable/mybutton" android:gravity="center" android:singleLine="true" android:text="Reset" android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="@color/black" android:textSize="8sp"/>
                    </LinearLayout>
                </LinearLayout>
                <LinearLayout android:id="@+id/LinearLayout02" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginBottom="5dp" android:layout_weight="1">
                    <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center" android:layout_marginLeft="4dp" android:layout_weight="5">
                        <TextView android:id="@+id/rowcounter" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center" android:gravity="center" android:text="0" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="@color/white1" android:textSize="44sp"/>
                    </LinearLayout>
                    <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center" android:layout_weight="3">
                        <Button android:id="@+id/rowup" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center_vertical" android:layout_marginRight="3dp" android:background="@drawable/mybutton" android:gravity="center" android:text="+" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="@color/black"/>
                    </LinearLayout>
                    <LinearLayout android:id="@+id/LinearLayout1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center" android:layout_marginRight="3dp" android:layout_weight="2" android:gravity="center_vertical" android:orientation="vertical">
                        <Button android:id="@+id/rowdown" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/mybutton" android:gravity="center_vertical|center_horizontal" android:singleLine="false" android:text="-" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="@color/black"/>
                    </LinearLayout>
                </LinearLayout>
            </LinearLayout>
            <LinearLayout android:id="@+id/counter_pat_section_row2" android:layout_width="match_parent" android:layout_height="108dp" android:layout_marginTop="1dp" android:background="@drawable/shape" android:orientation="vertical">
                <LinearLayout android:id="@+id/LinearLayout08" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="4dp" android:layout_weight="1">
                    <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center" android:layout_marginLeft="4dp" android:layout_weight="5">
                        <TextView android:id="@+id/TextView04" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginRight="2dp" android:gravity="right" android:text="Stop at row2" android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="@color/white1"/>
                    </LinearLayout>
                    <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center" android:layout_weight="3">
                        <EditText android:id="@+id/rowmax2" android:layout_width="fill_parent" android:layout_height="40dp" android:layout_gravity="center_vertical" android:layout_marginRight="3dp" android:layout_weight="1" android:inputType="number" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="@color/black"/>
                    </LinearLayout>
                    <LinearLayout android:id="@+id/LinearLayout07" android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center_vertical" android:layout_marginRight="3dp" android:layout_weight="2" android:gravity="center_vertical" android:orientation="vertical">
                        <Button android:id="@+id/Button01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:background="@drawable/mybutton" android:gravity="center" android:singleLine="true" android:text="Reset" android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="@color/black" android:textSize="8sp"/>
                    </LinearLayout>
                </LinearLayout>
                <LinearLayout android:id="@+id/LinearLayout06" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginBottom="5dp" android:layout_weight="1">
                    <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center" android:layout_marginLeft="4dp" android:layout_weight="5">
                        <TextView android:id="@+id/TextView02" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center" android:gravity="center" android:text="0" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="@color/white1" android:textSize="44sp"/>
                    </LinearLayout>
                    <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center" android:layout_weight="3">
                        <Button android:id="@+id/Button03" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center_vertical" android:layout_marginRight="3dp" android:background="@drawable/mybutton" android:gravity="center" android:text="+" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="@color/black"/>
                    </LinearLayout>
                    <LinearLayout android:id="@+id/LinearLayout07" android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center" android:layout_marginRight="3dp" android:layout_weight="2" android:gravity="center_vertical" android:orientation="vertical">
                        <Button android:id="@+id/Button02" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/mybutton" android:gravity="center_vertical|center_horizontal" android:singleLine="false" android:text="-" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="@color/black"/>
                    </LinearLayout>
                </LinearLayout>
            </LinearLayout>
            <LinearLayout android:id="@+id/counter_pat_section_row3" android:layout_width="match_parent" android:layout_height="108dp" android:layout_marginTop="1dp" android:background="@drawable/shape" android:orientation="vertical">
                <LinearLayout android:id="@+id/LinearLayout09" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="4dp" android:layout_weight="1">
                    <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center" android:layout_marginLeft="4dp" android:layout_weight="5">
                        <TextView android:id="@+id/TextView06" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginRight="2dp" android:gravity="right" android:text="Stop at row3" android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="@color/white1"/>
                    </LinearLayout>
                    <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center" android:layout_weight="3">
                        <EditText android:id="@+id/rowmax3" android:layout_width="fill_parent" android:layout_height="40dp" android:layout_gravity="center_vertical" android:layout_marginRight="3dp" android:layout_weight="1" android:inputType="number" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="@color/black"/>
                    </LinearLayout>
                    <LinearLayout android:id="@+id/LinearLayout11" android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center_vertical" android:layout_marginRight="3dp" android:layout_weight="2" android:gravity="center_vertical" android:orientation="vertical">
                        <Button android:id="@+id/Button05" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:background="@drawable/mybutton" android:gravity="center" android:singleLine="true" android:text="Reset" android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="@color/black" android:textSize="8sp"/>
                    </LinearLayout>
                </LinearLayout>
                <LinearLayout android:id="@+id/LinearLayout12" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginBottom="5dp" android:layout_weight="1">
                    <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center" android:layout_marginLeft="4dp" android:layout_weight="5">
                        <TextView android:id="@+id/TextView05" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center" android:gravity="center" android:text="0" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="@color/white1" android:textSize="44sp"/>
                    </LinearLayout>
                    <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center" android:layout_weight="3">
                        <Button android:id="@+id/Button04" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center_vertical" android:layout_marginRight="3dp" android:background="@drawable/mybutton" android:gravity="center" android:text="+" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="@color/black"/>
                    </LinearLayout>
                    <LinearLayout android:id="@+id/LinearLayout11" android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center" android:layout_marginRight="3dp" android:layout_weight="2" android:gravity="center_vertical" android:orientation="vertical">
                        <Button android:id="@+id/Button06" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/mybutton" android:gravity="center_vertical|center_horizontal" android:singleLine="false" android:text="-" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="@color/black"/>
                    </LinearLayout>
                </LinearLayout>
            </LinearLayout>
            <LinearLayout android:id="@+id/counter_pat_section_row4" android:layout_width="match_parent" android:layout_height="108dp" android:layout_marginTop="1dp" android:background="@drawable/shape" android:orientation="vertical">
                <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="4dp" android:layout_weight="1">
                    <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center" android:layout_marginLeft="4dp" android:layout_weight="5">
                        <TextView android:id="@+id/TextView07" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginRight="2dp" android:gravity="right" android:text="Stop at row4" android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="@color/white1"/>
                    </LinearLayout>
                    <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center" android:layout_weight="3">
                        <EditText android:id="@+id/rowmax4" android:layout_width="fill_parent" android:layout_height="40dp" android:layout_gravity="center_vertical" android:layout_marginRight="3dp" android:layout_weight="1" android:inputType="number" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="@color/black"/>
                    </LinearLayout>
                    <LinearLayout android:id="@+id/LinearLayout14" android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center_vertical" android:layout_marginRight="3dp" android:layout_weight="2" android:gravity="center_vertical" android:orientation="vertical">
                        <Button android:id="@+id/Button07" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:background="@drawable/mybutton" android:gravity="center" android:singleLine="true" android:text="Reset" android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="@color/black" android:textSize="8sp"/>
                    </LinearLayout>
                </LinearLayout>
                <LinearLayout android:id="@+id/LinearLayout13" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginBottom="5dp" android:layout_weight="1">
                    <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center" android:layout_marginLeft="4dp" android:layout_weight="5">
                        <TextView android:id="@+id/TextView08" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center" android:gravity="center" android:text="0" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="@color/white1" android:textSize="44sp"/>
                    </LinearLayout>
                    <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center" android:layout_weight="3">
                        <Button android:id="@+id/Button09" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center_vertical" android:layout_marginRight="3dp" android:background="@drawable/mybutton" android:gravity="center" android:text="+" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="@color/black"/>
                    </LinearLayout>
                    <LinearLayout android:id="@+id/LinearLayout14" android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center" android:layout_marginRight="3dp" android:layout_weight="2" android:gravity="center_vertical" android:orientation="vertical">
                        <Button android:id="@+id/Button08" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/mybutton" android:gravity="center_vertical|center_horizontal" android:singleLine="false" android:text="-" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="@color/black"/>
                    </LinearLayout>
                </LinearLayout>
            </LinearLayout>
            <LinearLayout android:id="@+id/counter_pat_section_repeat" android:layout_width="match_parent" android:layout_height="108dp" android:layout_marginTop="1dp" android:background="@drawable/shape" android:orientation="vertical">
                <LinearLayout android:id="@+id/LinearLayout04" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="4dp" android:layout_weight="1" android:weightSum="10">
                    <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center" android:layout_marginLeft="4dp" android:layout_weight="5">
                        <TextView android:id="@+id/TextView03" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginRight="2dp" android:gravity="right" android:singleLine="false" android:text="Stop at repeat" android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="@color/white1"/>
                    </LinearLayout>
                    <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center" android:layout_weight="3">
                        <EditText android:id="@+id/repeatmax" android:layout_width="fill_parent" android:layout_height="40dp" android:layout_gravity="center_vertical" android:layout_marginRight="3dp" android:layout_weight="1" android:inputType="number" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="@color/black">
                            <requestFocus />
                        </EditText>
                    </LinearLayout>
                    <LinearLayout android:id="@+id/LinearLayout1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center" android:layout_marginRight="3dp" android:layout_weight="2" android:gravity="center_vertical" android:orientation="vertical">
                        <Button android:id="@+id/resetrepeat" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:background="@drawable/mybutton" android:singleLine="true" android:text="Reset" android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="@color/black" android:textSize="8sp"/>
                    </LinearLayout>
                </LinearLayout>
                <LinearLayout android:id="@+id/LinearLayout03" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginBottom="5dp" android:layout_weight="1" android:weightSum="10">
                    <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center" android:layout_marginLeft="4dp" android:layout_weight="5">
                        <TextView android:id="@+id/repeatcounter" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center" android:gravity="center" android:text="0" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="@color/white1" android:textSize="44sp"/>
                    </LinearLayout>
                    <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center" android:layout_weight="3">
                        <Button android:id="@+id/repeatup" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="right" android:layout_marginRight="3dp" android:layout_weight="1" android:background="@drawable/mybutton" android:text="+" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="@color/black"/>
                    </LinearLayout>
                    <LinearLayout android:id="@+id/LinearLayout1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center" android:layout_marginRight="3dp" android:layout_weight="2" android:gravity="center_vertical" android:orientation="vertical">
                        <Button android:id="@+id/repeatdown" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/mybutton" android:gravity="center" android:text="-" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="@color/black"/>
                    </LinearLayout>
                </LinearLayout>
            </LinearLayout>
            <LinearLayout android:id="@+id/counter_pat_section_checkbox" android:layout_width="match_parent" android:layout_height="40dp">
                <CheckBox android:id="@+id/linkcounters" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="1" android:text="Link Counts" android:textColor="@color/white1" android:textSize="10sp"/>
                <CheckBox android:id="@+id/screenon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="1" android:text="Keep Awake" android:textColor="@color/white1" android:textSize="10sp"/>
                <CheckBox android:id="@+id/soundon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:singleLine="false" android:text="\u266A" android:textColor="@color/white1" android:textSize="16sp"/>
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</LinearLayout>
4

2 回答 2

1

这是我的 hack,效果很好....

    sv = (ScrollView) this.findViewById(R.id.counter_pat_scrollview);
    sv.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
    sv.setFocusable(true);
    sv.setFocusableInTouchMode(true);
    sv.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (rowmax1.hasFocus()) { rowmax1.clearFocus(); }
            if (rowmax2.hasFocus()) { rowmax2.clearFocus(); }
            if (rowmax3.hasFocus()) { rowmax3.clearFocus(); }
            if (rowmax4.hasFocus()) { rowmax4.clearFocus(); }
            return false;
        }
    });

在此示例中,rowmax1、2、3 等是 EditText。

当视图高度较大(垂直滚动视图)并且滚动视图包含编辑文本时,这将允许滚动视图滚动到屏幕边界之外。

并且它不会阻止用户按下编辑文本并获得软键盘。

希望这最终能帮助别人......如果可以的话,请标记我?

于 2013-02-27T10:13:42.063 回答
0

我发现 layout_weight 使 edittext 行为不端。我只是暂时停止使用此参数,直到有更多关于处理此案的信息。

于 2019-04-03T18:35:27.527 回答