添加和删​​除TextView Android的删除线

时间:2014-06-19 10:32:19

标签: android android-checkbox

大家好,我在textview中遇到了删除问题的问题。这就是我现在正在做的事情:

  private void initControls() {

    tvNote = (TextView) findViewById (R.id.textView1);
    tvNote.setVisibility(View.GONE);

    fillData();

}

private void fillData() {

      dbConnect();

      // Array list of sync data items
      ArrayList<SyncDataItems> itemList = new ArrayList<SyncDataItems>();

      List<Todos> todos = dbHelper.getAllTodos();      

      SyncDataItems _items;

      for (Todos cn : todos) {
          String log = "ID: " + cn.getID() + " , Todo: " + cn.getTodo();
          Log.e("Todo List ", log);
          _items = new SyncDataItems(cn.getTodo(), cn.getTodo(), true);
          itemList.add(_items);
      }

      dataAdapter = new MyCustomAdapter(this, R.layout.item_info, itemList);
      listView = (ListView) findViewById(R.id.listView1);
      // Assign adapter to ListView
      listView.setAdapter(dataAdapter);

      if(dataAdapter.getCount() == 0) {
          tvNote.setVisibility(View.VISIBLE);
          tvNote.setText("No Records Yet");
      }


}

  private class MyCustomAdapter extends ArrayAdapter<SyncDataItems> {

      private ArrayList<SyncDataItems> itemList;

      public MyCustomAdapter(Context context, int textViewResourceId,

      ArrayList<SyncDataItems> itemList) {
       super(context, textViewResourceId, itemList);
           this.itemList = new ArrayList<SyncDataItems>();
           this.itemList.addAll(itemList);
      }

      private class ViewHolder {
           TextView code;
           CheckBox name;
      }

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

            holder = null;

            Log.v("ConvertView", String.valueOf(position));

            if (convertView == null) {

                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                convertView = vi.inflate(R.layout.item_info, null);

                holder = new ViewHolder();
                holder.code = (TextView) convertView.findViewById(R.id.code);
                holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);


                convertView.setTag(holder);

                holder.name.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        CheckBox cb = (CheckBox) v;
                        SyncDataItems _item = (SyncDataItems) cb.getTag();

                        _item.setSelected(cb.isChecked());


                        holder.name.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                                if ( isChecked ) {
                                    Log.e("TAG", "Checked!");
                                    holder.code.append("Checked!");
                                    holder.code.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
                                } else {
                                    holder.code.setPaintFlags(holder.code.getPaintFlags() & (~ Paint.STRIKE_THRU_TEXT_FLAG));
                                    Log.e("TAG", "Unchecked!");
                                }
                            }
                        });

                    }
                });

            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            final SyncDataItems item = itemList.get(position);

            holder.code.setText(item.getItem());
            holder.name.setChecked(item.isSelected());

            holder.name.setTag(item);



            return convertView;
        }

}

但是当我选中或取消选中复选框时,删除线效果不起作用。有任何想法吗?我们将非常感谢您的帮助。感谢。

3 个答案:

答案 0 :(得分:2)

您可以使用:

holder.code.setPaintFlags(tv.getPaintFlags() & (~ Paint.STRIKE_THRU_TEXT_FLAG));

答案 1 :(得分:0)

On Checked,更改

holder.code.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);

holder.code.setPaintFlags(holder.code.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

它应该有用。

答案 2 :(得分:0)

通过任何文字删除警示的最短路径

tvNote.setPaintFlags(0);