求助 ScrollView子view变化后自动滑动到edittext焦点位置问题

just2learn 2016-08-19 01:48:02
最近在做一个笔记应用时需要在用ScrollView套上linearlayout当父控件,其中的子控件有editText用来输入文本,但是发现在其他子控件刷新时,如果当前看不到editText的焦点,scrollView会自动跳到editText的焦点位置,这个问题无法解决,demo代码如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/linear_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"
android:descendantFocusability="beforeDescendants" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
<EditText
android:id="@+id/et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>


public class MainActivity extends Activity {

private TextView tv;
private EditText et;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) this.findViewById(R.id.tv);
et = (EditText) this.findViewById(R.id.et);
handler.sendEmptyMessageDelayed(0, 1000);
Window window = this.getWindow();
}

private Handler handler = new Handler() {

@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
tv.setText(""+System.currentTimeMillis());
this.sendEmptyMessageDelayed(0, 1000);
}
};

}

这里textView每次setText时都会引起scrollView滑到editText的光标位置处。但是参考中兴笔记,如图,是可以在保持editText存在焦点的状态下还能同时刷新录音控件,而且scrollView不会自动滑动。请教大牛如何才能实现这种效果呢,谢谢!!!

...全文
1648 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_33220370 2017-12-21
  • 打赏
  • 举报
回复
外部滑屏控件优先获取焦点,editText就会失去焦点,不会下滑了 private void test() { entryScroll.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS); entryScroll.setFocusable(true); entryScroll.setFocusableInTouchMode(true); entryScroll.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { v.requestFocusFromTouch(); return false; } }); }
yeluoxiaoxin 2017-07-07
  • 打赏
  • 举报
回复
解决了吗?如果是 多个edittext呢
boolean_ 2017-06-05
  • 打赏
  • 举报
回复
fix 滚动 , 重写EditText @Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_MOVE) this.getParent().requestDisallowInterceptTouchEvent(true); return super.onTouchEvent(event); } fix 焦点跳动, 重写ScrollView @Override public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) { return child instanceof EditText; }
zpf82118 2016-12-30
  • 打赏
  • 举报
回复
解决了吗?我也遇到这个问题
just2learn 2016-08-19
  • 打赏
  • 举报
回复
网上的解决方案一般是禁用了scrollView的自动滑动功能,但是我还需要在editText中输入文本时,ScrollView能够正常的根据文本光标位置进行滑动,在输入时保证能看到光标。

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧