当前位置: 移动技术网 > IT编程>移动开发>Android > Android 中CheckBox多项选择当前的position信息提交的示例代码

Android 中CheckBox多项选择当前的position信息提交的示例代码

2019年07月24日  | 移动技术网IT编程  | 我要评论

星神祭txt,何苗淘宝,爱米吱

先给大家展示下效果图:

废话不多说了,下面通过示例代码给大家介绍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信息提交的示例代码,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网