listview checkbox edittext 的共同使用

最近项目需求中有一个列表输入框,listview item中有checkbox edittext 共同使用其中的焦点问题和监听edittext输入内容的变化困扰了我好久,基础太差,没办法,无从下手,就从网上找了一些第三方开源的库来使用,结果都没有符合要求的,很是苦恼,终于功夫不负有心人,自己在CSDN的博客里面直接搜索,就找到了自己改了一下就完全符合要求了,感谢开源,感谢CSDN!下面就上代码:

item布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:orientation="vertical" >

    <!-- android:descendantFocusability="blocksDescendants" -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <CheckBox
            android:id="@+id/checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dip"
            android:checked="true"
            android:focusable="false"
            android:focusableInTouchMode="false" />

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:minWidth="50dip"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dip"
            android:gravity="center"
            android:text="测试数据"
            android:textColor="#666666"/>

    </LinearLayout>


    <!--<Button-->
        <!--android:id="@+id/downbtn"-->
        <!--android:layout_width="60dip"-->
        <!--android:layout_height="wrap_content"-->
        <!--android:layout_marginLeft="10dip"-->
        <!--android:focusable="false"-->
        <!--android:focusableInTouchMode="false"-->
        <!--android:text="-" />-->

    <EditText
        android:textColor="@android:color/black"
        android:id="@+id/num_edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dip"
        android:singleLine="true"
        android:text=""
        android:gravity="center"
        android:textSize="35dip"
        android:background="#e0ffff"/>

    <!--<Button-->
        <!--android:id="@+id/upbtn"-->
        <!--android:layout_width="60dip"-->
        <!--android:layout_height="wrap_content"-->
        <!--android:layout_marginLeft="5dip"-->
        <!--android:focusable="false"-->
        <!--android:focusableInTouchMode="false"-->
        <!--android:text="+" />-->

</LinearLayout>

主布局代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:cacheColorHint="#00000000"
        android:choiceMode="singleChoice"
        android:listSelector="@android:color/transparent"
        android:divider="#DDDDDD"
        android:dividerHeight="2dip"
        android:focusableInTouchMode="true"
        android:longClickable="true"
        android:layout_marginTop="10dip"
        android:layout_marginBottom="10dip"
        android:scrollbarSize="50dp"
        android:scrollbarStyle="outsideOverlay"/>

    <!--<LinearLayout -->
        <!--android:layout_width="fill_parent"-->
        <!--android:layout_height="wrap_content"-->
        <!--android:orientation="horizontal">-->
        <!---->
        <!--<Button -->
            <!--android:id="@+id/btn_selectAll"-->
            <!--android:layout_width="wrap_content"-->
            <!--android:layout_height="wrap_content"-->
            <!--android:layout_weight="1"-->
            <!--android:text="选择全部"/>-->
        <!---->
        <!--<Button -->
            <!--android:id="@+id/btn_cancleselectall"-->
            <!--android:layout_width="wrap_content"-->
            <!--android:layout_height="wrap_content"-->
            <!--android:layout_weight="1"-->
            <!--android:text="反选"/>-->
        <!---->
        <!--<Button -->
            <!--android:id="@+id/btn_cancelAll"-->
            <!--android:layout_width="wrap_content"-->
            <!--android:layout_height="wrap_content"-->
            <!--android:layout_weight="1"-->
            <!--android:text="取消全部"/>-->
        <!---->
    <!--</LinearLayout>-->

</LinearLayout>

其中最主要的代码就是在自己自定义的Adapter 里面了,因为布局完全是自定义的,对里面的字控件的操作和控制就只能写在Adapter里面了,代码:

package com.example.listvievedittextcheckbox;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;

public class ListEditorAdapter extends BaseAdapter {

private LayoutInflater mInflater;
private Context mContext;
private List<Map<String, String>> mData; // �洢��editTexֵ
private Map<String, String> editorValue = new HashMap<String, String>();
private static HashMap<Integer, Boolean> isSelected;

public ListEditorAdapter(Context context) {
    // TODO Auto-generated constructor stub
    mInflater = LayoutInflater.from(context);
}

public void setData( List<Map<String, String>> data) {
    mData = data;
    init();
}

private void init() {
    editorValue.clear();
    isSelected = new HashMap<Integer, Boolean>();
    if (mData != null) {
        for (int i = 0; i < mData.size(); i++) {
            isSelected.put(i, false);
        }
    }
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    if (mData != null) {
        return mData.size();
    }
    return 0;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

private Integer index = -1;

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Log.d("zhang", "position = " + position);
    final ViewHolder holder;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_item, null);
        holder = new ViewHolder();
        holder.checkBox = (CheckBox) convertView
                .findViewById(R.id.checkbox);
        holder.textView = (TextView) convertView.findViewById(R.id.text);

// holder.downbtn = (Button) convertView.findViewById(R.id.downbtn);
// holder.downbtn.setFocusable(false);
// holder.downbtn.setFocusableInTouchMode(false);
// holder.upbtn = (Button) convertView.findViewById(R.id.upbtn);
// holder.downbtn.setFocusable(false);
// holder.downbtn.setFocusableInTouchMode(false);
holder.numEdit = (EditText) convertView.findViewById(R.id.num_edit);
holder.numEdit.setTag(position);
holder.numEdit.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    index = (Integer) v.getTag();
                }
                return false;
            }
        });

        class MyTextWatcher implements TextWatcher {

            public MyTextWatcher(ViewHolder holder) {
                mHolder = holder;
            }

            private ViewHolder mHolder;

            @Override
            public void beforeTextChanged(CharSequence s, int start,
                                          int count, int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onTextChanged(CharSequence s, int start,
                                      int before, int count) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                if (s != null && !"".equals(s.toString())) {
                    int position = (Integer) mHolder.numEdit.getTag();
                    // 用来保存edit输入的变化值
                    mData.get(position).put("list_item_inputvalue",
                            s.toString());
                }else{
                    int position = (Integer) mHolder.numEdit.getTag();
                    mData.get(position).put("list_item_inputvalue","");
                }

            }
        }
        holder.numEdit.addTextChangedListener(new MyTextWatcher(holder));

// holder.downbtn.setOnClickListener(new OnClickListener() {
//
// @Override
// public void onClick(View v) {
// // TODO Auto-generated method stub
// int position = (Integer) holder.numEdit.getTag();
// Log.d(“zhang”, “clickposition = ” + position);
// String edittextStr = holder.numEdit.getText().toString();
// int num = Integer.parseInt(edittextStr);
// num–;
// mData.get(position).put(“list_item_inputvalue”, num+”“);
// holder.numEdit.setText(num+”“);
// }
// });

// holder.upbtn.setOnClickListener(new OnClickListener() {
//
// @Override
// public void onClick(View v) {
// // TODO Auto-generated method stub
// int position = (Integer) holder.numEdit.getTag();
// String edittextStr = holder.numEdit.getText().toString();
// int num = Integer.parseInt(edittextStr);
// num++;
// mData.get(position).put(“list_item_inputvalue”, num+”“);
// holder.numEdit.setText(num+”“);
// }
// });

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

    holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
            int position = (Integer) holder.numEdit.getTag();
            isSelected.put(position, isChecked);
        }
    });

    holder.checkBox.setChecked(isSelected.get(position));

    Object value = mData.get(position).get("list_item_inputvalue");
    if (value != null && !"".equals(value)) {
        holder.numEdit.setText(value.toString());
    } else {
        holder.numEdit.setText("");
    }
    holder.numEdit.clearFocus();
    if (index != -1 && index == position) {
        holder.numEdit.requestFocus();
    }
    return convertView;
}
//用来存储按钮的选中众状态
public HashMap<Integer,Boolean> getIsSelected() {
    return isSelected;
}
//用来控制listview的某一项被选中和其中的按钮状态保持一致
public void setIsSelected(HashMap<Integer,Boolean> isSelected) {
    this.isSelected = isSelected;
}

public class ViewHolder {
    CheckBox checkBox;
    TextView textView;

// Button downbtn;
EditText numEdit;
// Button upbtn;

}

}
在Activity中的调用代码:

public class MainActivity extends Activity  {

//  private Button selectAllBtn;
//  private Button deselectAllBtn;
//  private Button cancelSelectAll;

    private ListView listView;
    private ListEditorAdapter mAdapter;

    private List<Map<String, String>> mData = new ArrayList<Map<String,String>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);

//      selectAllBtn = (Button) findViewById(R.id.btn_selectAll);
//      selectAllBtn.setOnClickListener(this);
//      deselectAllBtn = (Button) findViewById(R.id.btn_cancleselectall);
//      deselectAllBtn.setOnClickListener(this);
//      cancelSelectAll = (Button) findViewById(R.id.btn_cancelAll);
//      cancelSelectAll.setOnClickListener(this);

        listView = (ListView) findViewById(R.id.list);
        mAdapter = new ListEditorAdapter(this);
        listView.setAdapter(mAdapter);
        for(int i = 0; i < 20; i++) {
            Map<String, String> item = new HashMap<String, String>();
            item.put("list_item_inputvalue", i+"");
            mData.add(item);
        }
        mAdapter.setData(mData);

    }

//  @Override
//  public void onClick(View v) {
//      // TODO Auto-generated method stub
//      switch (v.getId()) {
//      case R.id.btn_selectAll:
//          for (int i = 0; i < mData.size(); i++) {
//              mAdapter.getIsSelected().put(i, true);
//          }
//          mAdapter.notifyDataSetChanged();
//          break;
//      case R.id.btn_cancleselectall:
//          // ����list�ij��ȣ�����ѡ����Ϊδѡ��δѡ����Ϊ��ѡ
//          for (int i = 0; i < mData.size(); i++) {
//              if (mAdapter.getIsSelected().get(i)) {
//                  mAdapter.getIsSelected().put(i, false);
//              } else {
//                  mAdapter.getIsSelected().put(i, true);
//              }
//          }
//          mAdapter.notifyDataSetChanged();
//          break;
//      case R.id.btn_cancelAll:
//          for (int i = 0; i < mData.size(); i++) {
//              if (mAdapter.getIsSelected().get(i)) {
//                  mAdapter.getIsSelected().put(i, false);
//              }
//          }
//          mAdapter.notifyDataSetChanged();
//          break;
//      }
//  }

}

这样就可以实现,edittext 和 checkbox 的焦点冲突问题了
另外大家可以看到有许多被注释掉的代码,大家可以把被注释掉的代码取消注释运行一下看下效果(其中要修改一下字布局)本人根据原来的博主的改的,理解一般

下面附上原来的demo下载地址:
http://download.csdn.net/detail/xinqing08007/5210396 直接下载就行
或者在CSDN 官网上 搜索: listview checkbox edittext 结合;
然后选择这里写图片描述

下载即可;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值