单击edittext时android自动滚动 [英] android auto scroll when clicked on edittext
问题描述
当用户单击edittext时,我希望屏幕滚动以在屏幕中间显示edittext.因此,我尝试听触摸(setOnTouchListener,也尝试过onClick和onFocuse),然后试听平滑滚动到屏幕,将edittext放在屏幕中间.
When user click on edittext I want the screen to scroll to show the edittext in the middle of the screen. So I tried to listen to touch (setOnTouchListener, also tried with onClick and onFocuse) and then smoothScrollTo screen to put the edittext in the middle of the screen.
但是由于某些原因,当我将setOnTouchListener添加到edittext时,它根本无法获得焦点.
but for some reason when I add the setOnTouchListener to the edittext it doesn't get focus at all.
我需要解决什么?或我该如何实现?
what do I need to fix? or how can I achieve this?
这是setOnTouchListener代码,导致单击时编辑文本无法集中注意力:
this is the setOnTouchListener code that cause the edittext not to get focused when clicked:
et_email.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
ScrollView scrollView = (ScrollView)getView().findViewById(R.id.ScrollViewSendDetails);
scrollView.smoothScrollTo(0, et_email.getBottom());
return true;
}
});
推荐答案
从onTouch()
方法返回true
表示在此处消耗了触摸事件.要允许事件传播到View自己的触摸侦听器,您需要return false;
.
Returning true
from the onTouch()
method indicates that the touch event is being consumed there. To allow the event to propagate through to the View's own touch listener, you need to return false;
.
要在ScrollView
完成滚动后获得EditText
的焦点,可以将Runnable
张贴到ScrollView
的处理程序中,以请求将焦点放在EditText
上.例如:
To get the EditText
to gain focus after the ScrollView
has finished its scroll, you can post a Runnable
to the ScrollView
's handler to request focus on the EditText
. For example:
et_email.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
...
scrollView.smoothScrollTo(0, et_email.getTop());
scrollView.post(new Runnable() {
@Override
public void run() {
et_email.requestFocus();
}
}
);
return false;
}
}
);
这篇关于单击edittext时android自动滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!