用的EditText列表视图 - 自动滚动的"接下来" [英] Listview with edittext - auto scroll on "next"

查看:144
本文介绍了用的EditText列表视图 - 自动滚动的"接下来"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

予有每行上具有一个的EditText一个ListView(除了几个不可编辑的TextView年代)。当我编辑的EditText上的文字,软键盘上有下一步按钮 - 和pressing这将焦点移动到下一个字段 - 这是伟大的。在最后一排,按钮变为完成。

我用 EditText.setImeOptions 设置按钮为完成或下一步,在此基础上是否是最后的行或不行。

现在的问题是,该列表视图可以有能适合在屏幕上更行。当发生这种情况,就下可见行pressing下一步再次将焦点移动到第一行。我怎样才能使它滚动列表并转到下一行呢?

有关参考,下面就是我在做我的适配器:

 公共类AuditAdapter扩展了BaseAdapter {
    私人上下文的背景下;
    私人诠释layoutResourceId;
    私人审计的审计;

    ...

    @覆盖
    公共查看getView(最终诠释的立场,观点convertView,ViewGroup中父){
        查看排= convertView;
        最后AuditItemHolder架=(行== NULL新AuditItemHolder():?(AuditItemHolder)row.getTag());

        如果(行== NULL)
        {
            LayoutInflater充气= ...;
            行= inflater.inflate(layoutResourceId,父母,假);
            ...
            holder.qtyf =(EditText上)row.findViewById(R.id.item_quantity);
        }

        AuditItem项目= audit.getItemAt(位置);

        holder.qtyf.setText(+ item.getQuantity());
        holder.qtyf.setImeOptions(位置== audit.size() -  1 EditorInfo.IME_ACTION_DONE:EditorInfo.IME_ACTION_NEXT);

        ...

        row.setTag(保持器);
        返回行;
    }

    私有静态类AuditItemHolder {
        ...
        的EditText qtyf;
    }
}
 

解决方案

好了,挣扎了很长一段时间后,我终于找到了破解(不是一个妥善的解决办法),其适用于我的情况。在 getView 我的适配器,我添加了 onEditorActionListener 和里面:

  ediField.setOnEditorActionListener(新OnEditorActionListener(){
    @覆盖
    公共布尔onEditorAction(TextView的V,INT actionId,KeyEvent的事件){
        ListView的LV =(ListView控件)父母;
        如果(actionId == EditorInfo.IME_ACTION_NEXT和放大器;&安培;
           LV = NULL和放大器;!&安培;
           位置> = lv.getLastVisiblePosition()及和放大器;
           位置= audit.size() -  1){//审计对象保存适配器的数据
                lv.smoothScrollToPosition(位置+ 1);
                lv.postDelayed(新的Runnable(){
                    公共无效的run(){
                        TextView的nextField =(TextView中)holderf.qtyf.focusSearch(View.FOCUS_DOWN);
                        如果(nextField!= NULL){
                            nextField.requestFocus();
                        }
                    }
                },200);
                返回true;
        }
        返回false;
    }
});
 

I have a ListView with one EditText on each row (in addition to a couple of non-editable TextView's). When I'm editing the text in the EditText, the soft keyboard has "Next" button - and pressing it moves the focus to the next field - this is great. On the last row, the button changes to "Done".

I'm using EditText.setImeOptions to set the button to "Done" or "Next" based on whether this is the last row or not.

The problem is that the listview can have more rows that can fit on the screen. When that happens, pressing "Next" on the next visible row moves the focus onto the first row again. How can I make it scroll the list and go to the next row instead?

For reference, here's what I'm doing in my adapter:

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;
    }
}

解决方案

Ok, after struggling for a long time, I finally found a hack (not a proper solution) that works for my case. In the getView of my adapter, I add the onEditorActionListener and inside it:

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;
    }
});

这篇关于用的EditText列表视图 - 自动滚动的"接下来"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆