10

我有一个 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;
    }
}
4

2 回答 2

7

好的,经过长时间的努力,我终于找到了适合我的情况的 hack(不是正确的解决方案)。在getView我的适配器中,我在onEditorActionListener其中添加了 and:

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;
    }
});
于 2014-02-22T22:16:57.767 回答
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();
            }
          }
      }
});
于 2018-06-20T17:54:36.030 回答