在ListView适配器中对EditText进行验证

时间:2018-08-27 11:03:35

标签: android listview android-edittext android-adapter

我有一个ListView适配器,如下所示:

@Override
public View getView(int i, View view, ViewGroup viewGroup){
    if(layoutInflater == null){
        layoutInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    if(view == null){
        view = layoutInflater.inflate(R.layout.list_pickingscan, null);
    }

    final TextView tvMaterialCode = (TextView) view.findViewById(R.id.list_picking_scan_materialcode_tv);
    EditText etSerialNumber = (EditText) view.findViewById(R.id.list_picking_scan_serialnumber_tv);

    final PickingDetails pickingDetails = pickingDetailsList.get(i);
    tvMaterialCode.setText(pickingDetails.getMaterialCode().toString());
    etSerialNumber.setText(pickingDetails.getSerialNumber().toString());
    etSerialNumber.setMaxLines(1);
    return view;
}
}

视图应如下所示:

Material Code    Serial Number
Material A       ______________
Material B       ______________

每次用户在提交按钮之前输入值时,我想对EditText序列号进行验证。怎么做?

1 个答案:

答案 0 :(得分:2)

您可以使用 TextWatcher

添加验证
 private class GenericTextWatcher implements TextWatcher {

    private View view;

    private GenericTextWatcher(View view) {
        this.view = view;
    }

    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }

    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }

    public void afterTextChanged(Editable editable) {
        switch (view.getId()){
            case **First View Id:**
            //validation for first edit text
            break;
            case **second view id :**
            //validation for second edit text
             break
        }

    }
}

m_InputMobile.addTextChangedListener(new GenericTextWatcher(m_InputMobile));
secondView.addTextChangedListener(new GenericTextWatcher(secondView));