我有一个ListView,每行有一个EditText(除了一些不可编辑的TextView)。当我在EditText中编辑文本时,软键盘有“下一步”按钮 - 按下它会将焦点移动到下一个字段 - 这很棒。在最后一行,按钮变为“完成”。
我正在使用EditText.setImeOptions
根据这是否是最后一行将按钮设置为“完成”或“下一步”。
问题是列表视图可以包含更多可以放在屏幕上的行。发生这种情况时,在下一个可见行上按“下一步”会将焦点再次移动到第一行。如何让它滚动列表并转到下一行呢?
供参考,这是我在适配器中所做的事情:
public class AuditAdapter extends BaseAdapter {
private Context context;
private int layoutResourceId;
private Audit audit;
...
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
final AuditItemHolder holder = (row == null ? new AuditItemHolder() : (AuditItemHolder)row.getTag());
if(row == null)
{
LayoutInflater inflater = ...;
row = inflater.inflate(layoutResourceId, parent, false);
...
holder.qtyf = (EditText)row.findViewById(R.id.item_quantity);
}
AuditItem item = audit.getItemAt(position);
holder.qtyf.setText("" + item.getQuantity());
holder.qtyf.setImeOptions(position == audit.size() - 1 ? EditorInfo.IME_ACTION_DONE : EditorInfo.IME_ACTION_NEXT);
...
row.setTag(holder);
return row;
}
private static class AuditItemHolder {
...
EditText qtyf;
}
}
答案 0 :(得分:7)
好吧,经过长时间的挣扎,我终于找到了适合我案例的黑客(不是一个合适的解决方案)。在我的适配器的getView
中,我添加了onEditorActionListener
并在其中:
ediField.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
ListView lv = (ListView)parent;
if(actionId == EditorInfo.IME_ACTION_NEXT &&
lv != null &&
position >= lv.getLastVisiblePosition() &&
position != audit.size() - 1) { //audit object holds the data for the adapter
lv.smoothScrollToPosition(position + 1);
lv.postDelayed(new Runnable() {
public void run() {
TextView nextField = (TextView)holderf.qtyf.focusSearch(View.FOCUS_DOWN);
if(nextField != null) {
nextField.requestFocus();
}
}
}, 200);
return true;
}
return false;
}
});
答案 1 :(得分:0)
我对持有人一无所知,因此我找到了一种不用它的方法。 在getView内添加onEditorActionListener。
EditText editView = (EditText)convertView;
editView.editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(final TextView view, int actionId, KeyEvent event){
if(actionId == EditorInfo.IME_ACTION_NEXT){
int lastPos = lv.getLastVisiblePosition();
lv.smoothScrollToPosition(position + 1);
if(position >= lastPos){
lv.postDelayed(new Runnable(){
@Override
public void run(){
final int posTop = lv.getFirstVisiblePosition();
TextView nextView = (EditText)lv.getChildAt(position - posTop + 1);
nextView.requestFocus();
}
}, 200);
}
//Delay is not required
else{
final int posTop = lv.getFirstVisiblePosition();
TextView nextView = (EditText)lv.getChildAt(position - posTop + 1);
nextView.requestFocus();
}
}
}
});