单击展开列表视图的父项后,Andriod EditText失去焦点

时间:2018-11-02 04:11:20

标签: android expandablelistview

我有可扩展的列表视图,每个视图都有多个文本视图。为了保持跟踪编辑文本焦点及其数据,我致电 setOnFocus 侦听器。但是,只有当我更改textview焦点时,它才起作用。当我选择或单击“新父项”时,我将失去焦点。

代码示例

final ViewHolder holder;

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

 holder.editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {

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


        holder.editText.setText(map.get("ed_" + parent + "" + child));

        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            if(!hasFocus)
              map.put("ed_" + parent + "" + child, holder.editText.getText().toString());
        }

    });
}

如果我改变editText的焦点,代码可以正常工作,但是如果我单击下一个父项,它将无法存储

他们可以使用Key Up功能的任何方式吗?

1 个答案:

答案 0 :(得分:0)

如果要使用onKeyUp函数,则必须实现TextWatcher类。

EditText tv_filter = (EditText) findViewById(R.id.filter);

TextWatcher fieldValidatorTextWatcher = new TextWatcher() {
    @Override
    public void afterTextChanged(Editable s) {
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (filterLongEnough()) {
            populateList();
        }
    }

    private boolean filterLongEnough() {
        return tv_filter.getText().toString().trim().length() > 2;
    }
};
tv_filter.addTextChangedListener(fieldValidatorTextWatcher);