先给大家展示下效果图:
废话不多说了,下面通过示例代码给大家介绍checkBox 多项选择当前的position信息提交,具体代码如下所示:
package com.dplustours.b2c.VIEw.activity; import androID.content.Context; import androID.vIEw.LayoutInflater; import androID.vIEw.VIEw; import androID.vIEw.VIEwGroup; import androID.Widget.AdapterVIEw; import androID.Widget.BaseAdapter; import androID.Widget.button; import androID.Widget.CheckBox; import androID.Widget.Toast; import com.dplustours.b2c.R; import com.dplustours.b2c.VIEw.application.MyApplication; import com.dplustours.b2c.VIEw.application.UIHelper; import java.util.ArrayList; import java.util.HashMap; /** * Created by zhq_zhao on 2017/7/7. */ public class ElseSelectCarinfosActivity extends BaseActivity implements VIEw.OnClickListener { private com.dplustours.b2c.VIEw.vIEw.MyListVIEw else_ListvIEw; private button next_step; private static CheckBox iv_select; private ArrayList elsetcarDetails; @OverrIDe protected String setheadStyleTitle() { return "其他选择"; } @OverrIDe protected voID requestData() { //租车信息列表 elsetcarDetails = new ArrayList(); MulAdapter mRentcarDetailsAdapter = new MulAdapter(this,elsetcarDetails); else_ListvIEw.setAdapter(mRentcarDetailsAdapter); elsetcarDetails.add(1); elsetcarDetails.add(1); elsetcarDetails.add(1); mRentcarDetailsAdapter.notifyDataSetChanged(); else_ListvIEw.setonItemClickListener(new AdapterVIEw.OnItemClickListener() { @OverrIDe public voID onItemClick(AdapterVIEw<?> parent,VIEw vIEw,int position,long ID) { // 取得VIEwHolder对象,这样就省去了通过层层的findVIEwByID去实例化我们需要的cb实例的步骤 MulAdapter.VIEwHolder vIEwHolder = (MulAdapter.VIEwHolder) vIEw.getTag(); vIEwHolder.iv_select.toggle();// 把CheckBox的选中状态改为当前状态的反,grIDvIEw确保是单一选中 MulAdapter.getIsSelected().put(position,vIEwHolder.iv_select.isChecked());////将CheckBox的选中状况记录下来 if (vIEwHolder.iv_select.isChecked() == true) { Toast.makeText(ElseSelectCarinfosActivity.this,"对勾"+position,Toast.LENGTH_LONG).show(); } else { Toast.makeText(ElseSelectCarinfosActivity.this,"取消"+position,Toast.LENGTH_LONG).show(); } Toast.makeText(ElseSelectCarinfosActivity.this,"当前"+position,Toast.LENGTH_LONG).show(); } }); } @OverrIDe protected VIEw getSuccessVIEw() { VIEw vIEw = VIEw.inflate(MyApplication.context,R.layout.activity_else_car,null); else_ListvIEw = (com.dplustours.b2c.VIEw.vIEw.MyListVIEw) vIEw.findVIEwByID(R.ID.else_ListvIEw); next_step = (button) vIEw.findVIEwByID(R.ID.next_step); next_step.setonClickListener(this); return vIEw; } @OverrIDe public voID onClick(VIEw v) { switch (v.getID()) { case R.ID.next_step: UIHelper.Go(ElseSelectCarinfosActivity.this,CarInfosOkActivity.class); break; default: break; } } public static class MulAdapter extends BaseAdapter { private LayoutInflater inflater = null;//导入布局 private Context context; //上下文 private ArrayList<String> List; // 控制CheckBox选中情况 private static HashMap<Integer,Boolean> isSelected; //导入布局 public MulAdapter(Context context,ArrayList<String> List) { this.context = context; this.List = List; inflater = LayoutInflater.from(context); isSelected = new HashMap<Integer,Boolean>(); initData(); } private voID initData() { //初始化isSelected的数据 for (int i = 0; i < List.size(); i++) { getIsSelected().put(i,false); } } @OverrIDe public int getCount() { return List.size(); } @OverrIDe public Object getItem(int position) { return List.get(position); } @OverrIDe public long getItemID(int position) { return position; } //ListvIEw每显示一行数据,该函数就执行一次 @OverrIDe public VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) { VIEwHolder holder = null; if (convertVIEw == null) { //当第一次加载ListVIEw控件时 convertVIEw为空 convertVIEw = inflater.inflate( R.layout.activity_elsecar_details,null); //所以当ListVIEw控件没有滑动时都会执行这条语句 holder = new VIEwHolder(); holder.iv_select = (CheckBox) convertVIEw.findVIEwByID(R.ID.iv_select); convertVIEw.setTag(holder);//为vIEw设置标签 } else { //取出holder holder = (VIEwHolder) convertVIEw.getTag(); //the Object stored in this vIEw as a tag } if (getIsSelected().get(position)!=null) { // 根据isSelected来设置checkBox的选中状况 holder.iv_select.setChecked(getIsSelected().get(position)); } return convertVIEw; } public class VIEwHolder { CheckBox iv_select; } public static HashMap<Integer,Boolean> getIsSelected() { return isSelected; } public voID setIsSelected(HashMap<Integer,Boolean> isSelected) { MulAdapter.isSelected = isSelected; } } } <?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:background="@color/weixin" androID:orIEntation="vertical"> <com.dplustours.b2c.VIEw.vIEw.InnerScrollVIEw androID:ID="@+ID/scrollVIEw" androID:layout_wIDth="match_parent" androID:layout_height="0dp" androID:layout_weight="1"> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical"> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="@dimen/dp_60" androID:layout_gravity="center" androID:background="@color/white" androID:gravity="center" androID:orIEntation="horizontal" androID:paddingleft="@dimen/dp_10"> <TextVIEw androID:layout_wIDth="0dp" androID:layout_height="match_parent" androID:layout_weight="1" androID:gravity="center|left" androID:text="车辆租金" androID:textcolor="@color/black" androID:textSize="@dimen/sp_15" /> <TextVIEw androID:layout_wIDth="0dp" androID:layout_height="match_parent" androID:layout_weight="3" androID:gravity="center" androID:text="¥ 6000.00×1=¥ 600.00" androID:textcolor="@color/gray" androID:textSize="@dimen/sp_13" /> </linearLayout> <VIEw androID:layout_wIDth="match_parent" androID:layout_height="@dimen/dp_1" androID:background="@color/login_line" /> <com.dplustours.b2c.VIEw.vIEw.MyListVIEw androID:ID="@+ID/else_ListvIEw" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:background="@color/white"> </com.dplustours.b2c.VIEw.vIEw.MyListVIEw> <VIEw androID:layout_wIDth="match_parent" androID:layout_height="@dimen/dp_1" androID:background="@color/login_line" /> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="@dimen/dp_60" androID:layout_gravity="center|right" androID:background="@color/white" androID:gravity="center|right" androID:orIEntation="horizontal" androID:paddingleft="@dimen/dp_10"> <TextVIEw androID:layout_wIDth="0dp" androID:layout_height="match_parent" androID:layout_marginRight="@dimen/dp_10" androID:layout_weight="3" androID:gravity="center|right" androID:text="¥6000.00" androID:textcolor="@color/red" androID:textSize="@dimen/sp_15" /> </linearLayout> <VIEw androID:layout_wIDth="match_parent" androID:layout_height="@dimen/dp_1" androID:background="@color/login_line" /> </linearLayout> </com.dplustours.b2c.VIEw.vIEw.InnerScrollVIEw> <button androID:ID="@+ID/next_step" androID:layout_wIDth="match_parent" androID:layout_height="@dimen/dp_60" androID:layout_alignParentBottom="true" androID:layout_gravity="bottom" androID:background="@color/yuyue" androID:gravity="center" androID:text="下一步" androID:textcolor="#FFFFFF" androID:textSize="@dimen/sp_18" /> </linearLayout> <?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="wrap_content" androID:background="@color/white" androID:orIEntation="vertical"> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="@dimen/dp_60" androID:orIEntation="horizontal" androID:paddingleft="@dimen/dp_10"> <linearLayout androID:layout_wIDth="0dp" androID:layout_height="match_parent" androID:layout_weight="1" androID:orIEntation="horizontal"> <CheckBox androID:focusable="false" androID:clickable="false" androID:button="@null" androID:background="@drawable/cheBox_mult_select" androID:ID="@+ID/iv_select" androID:layout_wIDth="@dimen/dp_25" androID:gravity="center|left" androID:layout_height="@dimen/dp_25" androID:layout_gravity="center" /> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="match_parent" androID:layout_gravity="center" androID:gravity="center" androID:paddingleft="@dimen/dp_15" androID:text="手续费" androID:textcolor="@color/center_item_text" /> </linearLayout> <TextVIEw androID:layout_wIDth="0dp" androID:layout_height="match_parent" androID:layout_gravity="center" androID:layout_weight="3" androID:textcolor="@color/black" androID:gravity="center" androID:text="¥ 50.00" /> </linearLayout> </linearLayout> package com.dplustours.b2c.VIEw.activity; import androID.annotation.TargetAPI; import androID.content.Context; import androID.os.Build; import androID.os.Bundle; import androID.os.IBinder; import androID.support.v7.app.AppCompatActivity; import androID.vIEw.MotionEvent; import androID.vIEw.VIEw; import androID.vIEw.Window; import androID.vIEw.WindowManager; import androID.vIEw.inputmethod.inputMethodManager; import androID.Widget.button; import androID.Widget.EditText; import androID.Widget.FrameLayout; import androID.Widget.linearLayout; import androID.Widget.TextVIEw; import androID.Widget.Toast; import com.dplustours.b2c.R; import com.dplustours.b2c.Utils.NetUtil; import com.dplustours.b2c.VIEw.application.MyApplication; import com.zhy.http.okhttp.OkhttpUtils; /** * Created by zhq_zhao on 2017/4/7. * acticity的基类用来管理所有的activity */ public abstract class BaseActivity extends AppCompatActivity { private VIEw vIEw; private linearLayout ll_activity_base; private button btn_headTitle_leftback; private TextVIEw g_headTitle_Title_textvIEw; private FrameLayout tl_header_info; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //创建公共的vIEw就是头部样式 vIEw = VIEw.inflate(MyApplication.context,R.layout.activity_header_style_base,null); ll_activity_base = (linearLayout) vIEw.findVIEwByID(R.ID.ll_activity_base); btn_headTitle_leftback = (button) vIEw.findVIEwByID(R.ID.btn_headTitle_leftback); g_headTitle_Title_textvIEw = (TextVIEw) vIEw.findVIEwByID(R.ID.g_headTitle_Title_textvIEw); tl_header_info = (FrameLayout) vIEw.findVIEwByID(R.ID.tl_header_info); //这个是让子类去实现具体的vIEw VIEw successVIEw = getSuccessVIEw(); //在添加之前要让没数据的时候子vIEw也要填充满 linearLayout.LayoutParams params = new linearLayout.LayoutParams(linearLayout.LayoutParams.FILL_PARENT,linearLayout.LayoutParams.FILL_PARENT); ll_activity_base.addVIEw(successVIEw,params); //动态添加vIEw setContentVIEw(vIEw); //初始化头部样式的逻辑 intiheadStyle(btn_headTitle_leftback,g_headTitle_Title_textvIEw); //添加沉浸式状态栏 addStatlan(); //判断网络是否打开 if (NetUtil.isNetworkAvailable(MyApplication.context) == true) {//说明网络已经打开 //从服务器获取数据 requestData(); } else { Toast.makeText(MyApplication.context,"请连接网络",Toast.LENGTH_LONG).show(); } } private voID intiheadStyle(button btn_headTitle_leftback,TextVIEw g_headTitle_Title_textvIEw) { btn_headTitle_leftback.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { finishActivity(); } }); //头部样式设置标题让子类去实现 String headTileinfo = setheadStyleTitle(); g_headTitle_Title_textvIEw.setText(headTileinfo); } protected voID finishActivity(){ finish(); } /** * 返回头部字符串 * * @return */ protected abstract String setheadStyleTitle(); private voID addStatlan() { //判断SDK版本是否大于等于19,大于就让他显示,小于就要隐藏,不然低版本会多出来一个 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { setTranslucentStatus(true); tl_header_info.setVisibility(VIEw.VISIBLE); //还有设置VIEw的高度,因为每个型号的手机状态栏高度都不相同 } else { tl_header_info.setVisibility(VIEw.GONE); } } @TargetAPI(19) private voID setTranslucentStatus(boolean on) { Window win = getwindow(); WindowManager.LayoutParams winParams = win.getAttributes(); final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; if (on) { winParams.flags |= bits; } else { winParams.flags &= ~bits; } win.setAttributes(winParams); } /** * 初始化数据 * * @return */ protected abstract voID requestData(); /** * 创建自己的vIEw * * @return */ protected abstract VIEw getSuccessVIEw(); @OverrIDe protected voID onDestroy() { super.onDestroy(); OkhttpUtils.getInstance().cancelTag(MyApplication.context); } /** * 根据EditText所在坐标和用户点击的坐标相对比,来判断是否隐藏键盘,因为当用户点击EditText时没必要隐藏 * * @param v * @param event * @return */ public boolean isShouldHIDeinput(VIEw v,MotionEvent event) { if (v != null && (v instanceof EditText)) { int[] l = {0,0}; v.getLocationInWindow(l); int left = l[0],top = l[1],bottom = top + v.getHeight(),right = left + v.getWIDth(); if (event.getX() > left && event.getX() < right && event.getY() > top && event.getY() < bottom) { // 点击EditText的事件,忽略它。 return false; } else { return true; } } // 如果焦点不是EditText则忽略,这个发生在视图刚绘制完,第一个焦点不在EditVIEw上,和用户用轨迹球选择其他的焦点 return false; } /** * 多种隐藏软件盘方法的其中一种 * * @param token */ public voID hIDeSoftinput(IBinder token) { if (token != null) { inputMethodManager im = (inputMethodManager) getSystemService(Context.input_METHOD_SERVICE); im.hIDeSoftinputFromWindow(token,inputMethodManager.HIDE_NOT_ALWAYS); } } @OverrIDe public boolean dispatchtouchEvent(MotionEvent ev) { if (ev.getAction() == MotionEvent.ACTION_DOWN) { // 获得当前得到焦点的VIEw,一般情况下就是EditText(特殊情况就是轨迹求或者实体案件会移动焦点) VIEw v = getCurrentFocus(); if (isShouldHIDeinput(v,ev)) { hIDeSoftinput(v.getwindowToken()); } } return super.dispatchtouchEvent(ev); } }
以上所述是小编给大家介绍的AndroID 中CheckBox多项选择当前的position信息提交的示例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!
总结以上是内存溢出为你收集整理的Android 中CheckBox多项选择当前的position信息提交的示例代码全部内容,希望文章能够帮你解决Android 中CheckBox多项选择当前的position信息提交的示例代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)