我们如何将编辑文本值存储在hashText onTextChangedListner

时间:2018-11-12 09:52:38

标签: android android-edittext expandablelistview

我正在使用可扩展列表视图,其中每个子布局都有一个编辑文本。我为每个文本框触发 setOnFocusChangeListener ,以防止数据丢失。

但是我观察到我输入的文本仅在下一个编辑文本光标移动时才存储到哈希图中。但是我需要在输入文本时存储输入的文本,例如 onKeyUp

注意::我也尝试了Text Watchers,但无法正常工作

代码

public Map<String, String> mapping = new HashMap<>();

@Override
public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {

 ViewHolder holder;

 Map<String, String> item = (Map<String, String>) getChild(i, i1);
 String key = item.get("item_code");

 if (view == null) {
        holder = new ViewHolder();
        holder.editText = view.findViewById(R.id.txtRecordComment);
        view.setTag(holder);
 } else {
        holder = (ViewHolder) view.getTag();
 }


 holder.editText.setText(mapping.get(key));

 /*
 holder.editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            if (!hasFocus) {
                mapping.put(key, holder.editText.getText().toString());
            }
        }
    }); 
   */


   holder.editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                   mapping.put(key, holder.editText.getText().toString());
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });


    return view;
}

private class ViewHolder {
    protected EditText editText;
}

上述代码的问题是,如果我在parent1的textbox1中输入文本,则相同的文本将在parent2的任一文本框中被复制。

如果有人建议我如何实现这一目标,将会有很大帮助。

1 个答案:

答案 0 :(得分:1)

    @Override
    public View getChildView(final int viewPosition, final int i1, boolean b, View view, ViewGroup viewGroup) {

     ViewHolder holder;

     Map<String, String> item = (Map<String, String>) getChild(i, i1);
     String key = item.get("item_code");

   //  if (view == null) {
            holder = new ViewHolder();
            holder.editText = view.findViewById(R.id.txtRecordComment);
  //          view.setTag(holder);
   //  } else {
   //         holder = (ViewHolder) view.getTag();
   //  }


     holder.editText.setText(mapping.get(viewPosition));

     /*
     holder.editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (!hasFocus) {
                    mapping.put(viewPosition, holder.editText.getText().toString());
                }
            }
        }); 
       */


       holder.editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                       mapping.put(viewPosition, holder.editText.getText().toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });


        return view;
    }

    private class ViewHolder {
        protected EditText editText;
    }