我想为每个具有的EditText(在我的情况下为VerifiedEditText)设置自动滚动。 我是这样做的,但我不认为这是应该做的方式(看起来太复杂,凌乱且时间太长),请您推荐我使它更简单吗?我仍在独自学习,因此对附加代码的解释将是非常感谢。谢谢
@Override
public void onFocusScroll() {
final ScrollView mScrollView;
mScrollView = (ScrollView) rootView.findViewById(R.id.register_scroll);
final VerifiedEditText editText0 = (VerifiedEditText) rootView.findViewById(R.id.register_ticket);
final VerifiedEditText editText1 = (VerifiedEditText) rootView.findViewById(R.id.register_sektor);
final VerifiedEditText editText2 = (VerifiedEditText) rootView.findViewById(R.id.register_row);
final VerifiedEditText editText3 = (VerifiedEditText) rootView.findViewById(R.id.register_seat);
editText0.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
mScrollView.post(new Runnable() {
public void run() {
mScrollView.smoothScrollTo(0, mScrollView.getTop());
}
});
}
}
});
editText1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
mScrollView.post(new Runnable() {
@Override
public void run() {
mScrollView.smoothScrollTo(0, mScrollView.getBottom());
}
});
}
}
});
editText2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
mScrollView.post(new Runnable() {
@Override
public void run() {
mScrollView.smoothScrollTo(0, mScrollView.getBottom());
}
});
}
}
});
editText3.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
mScrollView.post(new Runnable() {
@Override
public void run() {
mScrollView.smoothScrollTo(0, mScrollView.getBottom());
}
});
}
}
});
}
答案 0 :(得分:0)
implement class by `View.OnFocusChangeListener`
and set Focus Listener to the current instanace of class because treat as View.OnFocusChangeListener
instnace
and Override onFocusChange()
medthod
v.getTop()
return top position of current focused view
editText0.setOnFocusChangeListener(this);
editText1.setOnFocusChangeListener(this);
editText2.setOnFocusChangeListener(this);
editText3.setOnFocusChangeListener(this);
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
mScrollView.post(new Runnable() {
public void run() {
mScrollView.smoothScrollTo(0, v.getTop());
}
});
}
}
}