sld22525482
「已注销」
2016-09-22 02:00
采纳率: 12.6%
浏览 1.9k
已采纳

listview 多个item嵌套多个edittext 如何对相应item的edittext进行操作

要求是在listview的三个item中分别输入ip地址 每个item 有四个edittext
然后添加方法switch case position = 0时

edt_input1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (s.length() == 3) {                       
                        edt_input2.requestFocus();                           
                    }
                }
            });

在第一个item的第一个edittext输入之后 焦点并没有跳到第一个item的第二个edittext上 而是跳到了最后一个item的第二个edittext上,请问一下这里要如何处理?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • qq_24827089
    flk_123 2016-09-22 02:44
    已采纳

    这里不要用全局变量,这样子editText当前保存的对象是不能确定是那个item的,你这里的editText2应该是保存最后一个item的对象,所以才会滚动最后一个item上,试试用Holder

    点赞 评论
  • qq_24827089
    flk_123 2016-09-22 02:20

    这样子看不出什么问题,贴一下getView()的代码

    点赞 评论
  • sld22525482
    「已注销」 2016-09-22 02:29

    public class Adapter extends BaseAdapter {
    private Context context = null;
    private EditText editText1, editText2, editText3, editText4;
    private RelativeLayout relativeLayout;

    public Adapter(Context context) {
        this.context = context;
    
    }
    
    @Override
    public int getCount() {
        return 3;
    }
    
    @Override
    public Object getItem(int position) {
        return null;
    }
    
    @Override
    public long getItemId(int position) {
        return 0;
    }
    
    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.item_layout, null);
        editText1 = (EditText) convertView.findViewById(R.id.edt1);
        editText2 = (EditText) convertView.findViewById(R.id.edt2);
        editText3 = (EditText) convertView.findViewById(R.id.edt3);
        relativeLayout = (RelativeLayout) convertView.findViewById(R.id.item_content);
        if (MainActivity.clickeditem == position) {
            relativeLayout.setBackgroundColor(Color.WHITE);
        }
        switch (position) {
            case 0:
                editText1.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
                    }
    
                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
    
                    }
    
                    @Override
                    public void afterTextChanged(Editable s) {
                        if (s.length() == 3) {
                            editText2.requestFocus();
                             //   editText1.focusSearch(View.FOCUS_DOWN);
                        }
                    }
                });
                break;
            case 1:
                break;
        }
        return convertView;
    }
    

    }

    
    
    点赞 评论
  • sld22525482
    「已注销」 2016-09-22 03:31
    private void setEdtIpInput() {
    
        EditeTextWatcher ipTextWatcher1 = new EditeTextWatcher(viewHolder.edt_input1);
        EditeTextWatcher ipTextWatcher2 = new EditeTextWatcher(viewHolder.edt_input2);
        EditeTextWatcher ipTextWatcher3 = new EditeTextWatcher(viewHolder.edt_input3);
        EditeTextWatcher ipTextWatcher4 = new EditeTextWatcher(viewHolder.edt_input4);
    
        viewHolder.edt_input1.addTextChangedListener(ipTextWatcher1);
        viewHolder.edt_input2.addTextChangedListener(ipTextWatcher2);
        viewHolder.edt_input3.addTextChangedListener(ipTextWatcher3);
        viewHolder.edt_input4.addTextChangedListener(ipTextWatcher4);
    
    }
    
    class EditeTextWatcher implements TextWatcher {
        public EditText mEditText;
    
        public EditeTextWatcher(EditText mEditText) {
            super();
            this.mEditText = mEditText;
        }
    
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
        }
    
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            Log.e("edt", "onTextChanged");
        }
    
        @Override
        public void afterTextChanged(Editable s) {
            if (s.length() == 3) {
                if (this.mEditText == viewHolder.edt_input1) {
                    viewHolder.edt_input2.requestFocus();
                } else if (this.mEditText == viewHolder.edt_input2) {
                    viewHolder.edt_input3.requestFocus();
                }
            } else if (s.length() == 1) {
                if (this.mEditText == viewHolder.edt_input3) {
                    viewHolder.edt_input4.requestFocus();
                }
            } else if (s.length() == 0) {
                if (this.mEditText == viewHolder.edt_input4) {
                    viewHolder.edt_input3.requestFocus();
                } else if (this.mEditText == viewHolder.edt_input3) {
                    viewHolder.edt_input2.requestFocus();
                } else if (this.mEditText == viewHolder.edt_input2) {
                    viewHolder.edt_input1.requestFocus();
                }
            }
        }
    }
    
    
    

    这个函数实在getview外面的,还有就是里面是没有viewHolder.的。。

    点赞 评论
  • qq_24827089
    flk_123 2016-09-22 04:13

    在getViewe 里ditText1.addTextChangedListener(new EditeTextWatcher (Holder));哪里要用的Holder就把getView里的Holder对象传过去就行了

    点赞 评论

相关推荐