我有一张包含大量EditTexts
的表单。点击EditText
(焦点)时,我希望scrollview
滚动到该位置。我在onFocusChangeListener
中实施了Fragment
,但它似乎无法正常工作。另一个问题是onFocusChange永远不会被调用..任何想法?
这是我的代码:
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
int[] loc = new int[2];
v.getLocationOnScreen(loc);
sw.scrollTo(loc[0], loc[1]);
}
}
答案 0 :(得分:0)
<强> edittext.requestFocus(); 强>
EditText editText = new EditText(getActivity());
editText.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
arg0.requestFocus();
}
});
答案 1 :(得分:0)
假设我们在片段内部实现了onFocusChangeListener 试试这个:
editText.setOnFocusChangeListener(this);
.....
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(v.getId()==editText.getId())
{
if(hasFocus){
int[] loc = new int[2];
v.getLocationOnScreen(loc);
sw.scrollTo(loc[0], loc[1]);
}
}
}
答案 2 :(得分:0)
你可以这样试试。比如
@Override
public void onCreate(Bundle savedInstanceState) {
.....
EditText et = (EditText)findViewById(R.id.wo_task_comments);
et.setOnTouchListener(this);
.....
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if(view.getId() == R.id.wo_task_comments){
view.getParent().requestDisallowInterceptTouchEvent(true);
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
view.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
}
return false;
}
在你的情况下:
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initComments(findViewById(R.id.YOUR_MAIN_LAYOUT_ID));
}
public void initComments(final View view) {
EditText comment = (EditText) view.findViewById(R.id.wo_task_comments);
comment.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(final View v, final MotionEvent motionEvent) {
if (v.getId() == R.id.wo_task_comments) {
v.getParent().requestDisallowInterceptTouchEvent(true);
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
v.getParent().requestDisallowInterceptTouchEvent(
false);
break;
}
}
return false;
}
});
comment.setText("very very long comment"
+ "very very long comment\n" + "very very long comment\n"
+ "very very long comment\n" + "very very long comment\n"
+ "very very long comment\n" + "very very long comment\n"
+ "very very long comment\n" + "very very long comment\n"
+ "very very long comment\n" + "very very long comment\n"
+ "very very long comment\n" + "very very long comment\n"
+ "very very long comment\n" + "very very long comment\n"
+ "very very long comment\n" + "very very long comment\n"
+ "very very long comment");
}
}
[参考] [在scrollview中滚动编辑框] 1