android scrollview焦点,android – 水平scrollview的问题自动在Edittext焦点上滚动

我正在使用包含相对布局的水平滚动视图,其中包含6个edittext.所有edittext都在视图中水平显示

MyXml.xml

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/horizontalView">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="horizontal"

>

android:id="@+id/sv1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

app:cardElevation="2dp"

app:cardUseCompatPadding="true">

android:layout_width="match_parent"

android:layout_height="match_parent"

>

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: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:layout_width="match_parent"

android:layout_height="match_parent">

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: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:layout_width="match_parent"

android:layout_height="match_parent">

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: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:layout_width="match_parent"

android:layout_height="match_parent">

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: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:layout_width="match_parent"

android:layout_height="match_parent">

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: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:layout_width="match_parent"

android:layout_height="match_parent">

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="" />

问题:现在每当我点击第一个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);

}

聚焦之前

ohixR.png

聚焦编辑文本1(或2,3或任何其他编辑文本后…水平滚动移动到自动上次.)

PxJch.png

但仍然水平滚动视图自动滚动到最后位置.我怎么能让它停下来?

解决方法:

我能在这里解决我的问题,但仍然不知道为什么会这样.

我删除了定义的editext的所有属性,并逐个添加了所有属性,并检查导致问题的属性,我发现当我再次将gravity =“center”属性添加到edittext时,水平滚动视图滚动到结束.

所以它正在发生因为gravitytext = edittext的“center”属性.

但我仍然不知道为什么edittext的引力直接影响水平滚动视图奇怪的行为.重力是否与父容器的内容视图有某种关系?

如果有人对此行为有回答,请分享您的观点.

标签:android,android-layout,horizontalscrollview

来源: https://codeday.me/bug/20190608/1200336.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值