我正在使用包含 Relative Layout
的 Horizontal Scroll View
,里面有 6 个 edittext
。显示所有 edittext
水平 View
MyXml.xml
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/horizontalView">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<!--android:weightSum="6"-->
<android.support.v7.widget.CardView
android:id="@+id/sv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardElevation="2dp"
app:cardUseCompatPadding="true">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<EditText
android:id="@+id/edt1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="end"
android:gravity="center"
android:hint="hintvalue"
android:imeOptions="actionNext"
android:ems="3"
android:inputType="number"
android:maxLength="2"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_toRightOf="@+id/sv1"
android:id="@+id/sv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardElevation="2dp"
app:cardUseCompatPadding="true">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edt2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="end"
android:gravity="center"
android:hint="hintvalue"
android:ems="4"
android:imeOptions="actionNext"
android:inputType="textCapCharacters"
android:maxLength="1"
android:singleLine="true"
android:text="" />
</android.support.design.widget.TextInputLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_toRightOf="@+id/sv2"
android:id="@+id/sv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardElevation="2dp"
app:cardUseCompatPadding="true">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edt3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="end"
android:gravity="center"
android:hint="hintvalue"
android:ems="4"
android:imeOptions="actionNext"
android:inputType="number"
android:maxLength="2"
android:singleLine="true"
android:text="" />
</android.support.design.widget.TextInputLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_toRightOf="@+id/sv3"
android:id="@+id/sv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardElevation="2dp"
app:cardUseCompatPadding="true">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edt4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="end"
android:gravity="center"
android:hint="hintvalue"
android:ems="3"
android:imeOptions="actionNext"
android:inputType="number"
android:maxLength="4"
android:singleLine="true"
android:text="" />
</android.support.design.widget.TextInputLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_toRightOf="@+id/sv4"
android:id="@+id/sv5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardElevation="2dp"
app:cardUseCompatPadding="true">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edt5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="end"
android:gravity="center"
android:hint="hintvalue"
android:ems="5"
android:imeOptions="actionNext"
android:inputType="number"
android:maxLength="1"
android:singleLine="true"
android:text="" />
</android.support.design.widget.TextInputLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_toRightOf="@+id/sv5"
android:id="@+id/sv6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardElevation="2dp"
app:cardUseCompatPadding="true">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edt6"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="end"
android:gravity="center"
android:hint="hintvalue"
android:ems="5"
android:inputType="number"
android:maxLength="3"
android:singleLine="true"
android:text="" />
</android.support.design.widget.TextInputLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
</HorizontalScrollView>
问题:现在每当我点击第一个 Edittext
(或任何编辑文本)时。 horizontal scrollview
自动滚动到结束位置。我不知道这种行为。为什么水平 ScrollView 会自动滚动?
我尝试过的
1) 我试图在第一次编辑文本焦点更改时将 smoothscroll
调用到 0,0 位置。
edt1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
if(b){
hv1.smoothScrollTo(0,0)
}
}
});
2) 我还尝试创建一个 自定义类
扩展 horizontal scrollview
并在 onLayout
覆盖方法中将位置更改为 0
public class HorizontalCustomScrollView extends android.widget.HorizontalScrollView {
private boolean enableScrolling = true;
public boolean isEnableScrolling() {
return enableScrolling;
}
public void setEnableScrolling(boolean enableScrolling) {
this.enableScrolling = enableScrolling;
}
public HorizontalCustomScrollView(Context context) {
super(context);
}
public HorizontalCustomScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public HorizontalCustomScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public HorizontalCustomScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onLayout (boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
this.scrollTo(0,0);
}
}
3) 我还尝试将 fullScroll
指向 FOCUS_LEFT
并在 onLayout
中覆盖 FOCUS_RIGHT
方法,但这也不起作用。
@Override
protected void onLayout (boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
//this.scrollTo(0,0);
this.fullScroll(HorizontalScrollView.FOCUS_LEFT);
//and also try with Focus right
//this.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
对焦前
关注编辑文本 1(或 2,3 或任何其他编辑文本..水平滚动自动移动到最后。)
但水平 ScrollView 仍然自动滚动到最后一个位置。我怎样才能让它停下来?
最佳答案
我可以在这里解决我的问题,但仍然不知道为什么会这样。
我删除了所有定义的 editext 属性,并逐一添加所有属性,并检查导致问题的属性,我发现当我添加 gravity="center"
属性再次水平 ScrollView 滚动到结束。
所以它的发生是因为 edittext 的 gravity="center"属性。
但我仍然不知道为什么 edittext 的引力会直接影响水平 ScrollView 的怪异行为。重力是否与父容器的内容 View 有某种关系?
如果有人对此行为有任何答案,请分享您的观点。
关于android - 水平 ScrollView 的问题自动滚动到 Edittext 焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37251253/