【问题标题】:Validation on EditText in ListView Adapter验证 ListView 适配器中的 EditText
【发布时间】:2019-02-01 21:45:35
【问题描述】:

我有一个 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 序列号进行验证。如何制作?

【问题讨论】:

    标签: android listview android-edittext android-adapter


    【解决方案1】:

    您可以使用 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));
    

    【讨论】:

    • 我如何知道根据您的回答选择了哪个位置的editText或editText?
    • 好的,让我更新多个editTexts的答案
    • 但我的问题还是一样,你怎么知道在哪个位置选择了哪个编辑文本? 假设您从 1 个位置选择editText,然后您选择了2 个位置,您怎么知道根据位置选择了哪个editText
    • 是的。如何知道 EditText 的位置?
    • 在为每个编辑文本创建对象时,将视图的引用传递给 GenericTextWatcher。因此,您可以获得执行 GenericTextWatcher 对象的视图的 ID,如下所示 switch (view.getId()){ case *First View Id: / /验证首次编辑文本中断; case second view id : //验证第二次编辑文本中断 }*
    猜你喜欢
    • 2016-01-14
    • 2018-12-13
    • 1970-01-01
    • 2012-09-22
    • 2018-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多