文本观察器不工作

Textwatcher not working

我有一个 listview,每行有 edittext,使用 textwatcher 来显示 toast 的内容输入。 textwatcher 适用于除第一行以外的所有行。输入文本后 toast 会在您输入文本后持续触发,而不是只显示一次。

其他行通过在每次更改输入时显示一次 toast 来很好地响应所需的行为。输入的默认值为 0.

这是我的 adaptergetView() 中的代码:

//tvItems is my edittext variable reference

TextWatcher tt = new TextWatcher() {

public void afterTextChanged(Editable s) {

                //tvItems.setSelection(s.length());
            }

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

            public void onTextChanged(CharSequence s, int start, int before, int count) {
                tvItems.removeTextChangedListener(tt);

                if (tvItems.getText().toString().trim().length() == 1) {

                  //to check if input is greater than 0 so we can proceed

                    if (Integer.valueOf(tvItems.getText().toString().trim()) > 0) {
                      //displays toast
                         Toast.makeText(activity, s, Toast.LENGTH_SHORT).show();


                        tvItems.removeTextChangedListener(tt);
                    }
                }
                tvItems.addTextChangedListener(tt);
            }
        };
        tvItems.addTextChangedListener(tt);

欢迎提出任何建议。

应要求,我的完整适配器代码:

public class ProductListAdapter extends BaseAdapter implements Filterable {
    List<ProductModel> mStringFilterList;
    ValueFilter valueFilter;
    int item_number = 0;
    TextWatcher tt = null;
    private Activity activity;
    private LayoutInflater inflater;
    private List<ProductModel> modelItems;
    private OnAddRemoveListener myaddInterface;
    TextView tvTitle;
    Button minusIcon, addIcon;

    public ProductListAdapter(Activity activity, List<ProductModel> modelItems, OnAddRemoveListener myaddInterface) {
        this.activity = activity;
        this.modelItems = modelItems;
        mStringFilterList = modelItems;
        this.myaddInterface = myaddInterface;
    }

    @Override
    public int getCount() {
        return modelItems.size();
    }

    @Override
    public Object getItem(int location) {
        return modelItems.get(location);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }/*
    @Override
    public boolean isEnabled(int position) {
        return false;
    }*/

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
            convertView = inflater.inflate(R.layout.product_item_row, null);

        final ProductModel m = modelItems.get(position);

        tvTitle = (TextView) convertView.findViewById(R.id.tv2);
        TextView tprice = (TextView) convertView.findViewById(R.id.tv3);
        boolean ignoreNextTextChange = true;
        final EditText tvItems = (EditText) convertView.findViewById(R.id.num);
        tvItems.setText("0");
        minusIcon = (Button) convertView.findViewById(R.id.minus);
        addIcon = (Button) convertView.findViewById(R.id.add);

        //add items
        addIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("add button", "clicked");
                int new_item_number = 1 + Integer.parseInt(tvItems.getText().toString());
                tvItems.setText("" + new_item_number);
                (myaddInterface).onItemadded(String.valueOf(m.getPrice()), new_item_number, position);
            }
        });

        minusIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("minus button", "clicked");

                int new_item_number = Integer.parseInt(tvItems.getText().toString());
                if (new_item_number > 0) {
                    (myaddInterface).onItemremoved(String.valueOf(m.getPrice()), position);
                    tvItems.setText("" + (new_item_number - 1));
                } else if (tvItems.getText().toString().equals("0")) {

                    tvItems.setText("0");
                }

            }
        });

        tvTitle.setText(m.getname());
        tprice.setText("(" + m.getPrice() + ")");

    public void afterTextChanged(Editable s) {
        if (tvItems.getText().toString().trim().length() == 1) { 
 
            //to check if input is greater than 0 so we can proceed 
            if (Integer.valueOf(tvItems.getText().toString().trim()) > 0) {
               //displays toast 
               Toast.makeText(activity, s, Toast.LENGTH_SHORT).show();    
            } 
         } 
    } 
 
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    } 
 
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    } 
}; 
        tvItems.addTextChangedListener(tt);
        return convertView;
    }

    //code for search filter, not associated with the question
    
    @Override
    public Filter getFilter() {
        if (valueFilter == null) {
            valueFilter = new ValueFilter();
        }
        return valueFilter;
    }

    private class ValueFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            if (constraint != null && constraint.length() > 0) {
                ArrayList<ProductModel> filterList = new ArrayList<>();
                for (int i = 0; i < mStringFilterList.size(); i++)
                    if ((mStringFilterList.get(i).getname().toUpperCase())
                            .contains(constraint.toString().toUpperCase()) || (mStringFilterList.get(i).getproductDesc().toUpperCase())
                            .contains(constraint.toString().toUpperCase())) {


                        ProductModel m = new ProductModel(mStringFilterList.get(i)
                                .getProduct_id(), mStringFilterList.get(i)
                                .getname(), mStringFilterList.get(i)
                                .getPrice(), mStringFilterList.get(i)
                                .getimage_url(), mStringFilterList.get(i)
                                .getproductDesc());

                        filterList.add(m);
                    }
                results.count = filterList.size();
                results.values = filterList;
            } else {
                results.count = mStringFilterList.size();
                results.values = mStringFilterList;
                //show no results
            }

           /* }else if((mStringFilterList.get(i).getproductDesc().toUpperCase())
                    .contains(constraint.toString().toUpperCase())){}*/

            return results;

        }

        @Override
        protected void publishResults(CharSequence constraint,
                                      FilterResults results) {
            modelItems = (List<ProductModel>) results.values;
            notifyDataSetChanged();
        }

    }

}

你能试试这个吗:

TextWatcher tt = new TextWatcher() {

    public void afterTextChanged(Editable s) {
        if (tvItems.getText().toString().trim().length() == 1) {

            //to check if input is greater than 0 so we can proceed
            if (Integer.valueOf(tvItems.getText().toString().trim()) > 0) {
               //displays toast
               Toast.makeText(activity, s, Toast.LENGTH_SHORT).show();    
            }
         }
    }

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

    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }
};

将侦听器对象设置为 edittext,(即)tt,像这样

editText.addTextChangedListener(tt);