android – 水平scrollview的问题自动在Edittext焦点上滚动
作者:互联网
我正在使用包含相对布局的水平滚动视图,其中包含6个edittext.所有edittext都在视图中水平显示
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(或任何edittext)时.水平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)我还尝试创建一个自定义类来扩展水平滚动视图,并在onLayout覆盖方法中将postion更改为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)我也尝试在fullLayout覆盖方法中指定fullScroll到FOCUS_LEFT以及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或任何其他编辑文本后…水平滚动移动到自动上次.)
但仍然水平滚动视图自动滚动到最后位置.我怎么能让它停下来?
解决方法:
我能在这里解决我的问题,但仍然不知道为什么会这样.
我删除了定义的editext的所有属性,并逐个添加了所有属性,并检查导致问题的属性,我发现当我再次将gravity =“center”属性添加到edittext时,水平滚动视图滚动到结束.
所以它正在发生因为gravitytext = edittext的“center”属性.
但我仍然不知道为什么edittext的引力直接影响水平滚动视图奇怪的行为.重力是否与父容器的内容视图有某种关系?
如果有人对此行为有回答,请分享您的观点.
标签:android,android-layout,horizontalscrollview 来源: https://codeday.me/bug/20190608/1200336.html