先给大家展示下效果图:
废话不多说了,下面通过示例代码给大家介绍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信息提交的示例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
# checkbox
# 多项选择
# position信息提交
# Android CheckBox中设置padding无效解决办法
# Android开发之CheckBox的简单使用与监听功能示例
# Android 中CheckBox的isChecked的使用实例详解
# Android开发手册自定义Switch开关按钮控件
# Android开关控件Switch的使用案例
# Android 自定义Switch开关按钮的样式实例详解
# Android UI控件Switch的使用方法
# Android单选按钮RadioButton的使用方法
# Android复选框CheckBox与开关按钮Switch及单选按钮RadioButton使用示例详
# 子类
# 给大家
# 多项
# 小编
# 自己的
# 状态栏
# 都不
# 让他
# 第一个
# 在此
# 也要
# 说了
# 不多
# 其他的
# 这条
# 要让
# 所示
# 没必要
# 多出
# 所述
相关文章:
宁波自助建站系统如何快速打造专业企业网站?
如何在沈阳梯子盘古建站优化SEO排名与功能模块?
标准网站视频模板制作软件,现在有哪个网站的视频编辑素材最齐全的,背景音乐、音效等?
c# 在高并发下使用反射发射(Reflection.Emit)的性能
C#如何在一个XML文件中查找并替换文本内容
电商网站制作多少钱一个,电子商务公司的网站制作费用计入什么科目?
高端智能建站公司优选:品牌定制与SEO优化一站式服务
mc皮肤壁纸制作器,苹果平板怎么设置自己想要的壁纸我的世界?
如何在万网主机上快速搭建网站?
北京网站制作费用多少,建立一个公司网站的费用.有哪些部分,分别要多少钱?
如何选择高效便捷的WAP商城建站系统?
如何通过VPS建站无需域名直接访问?
微信小程序制作网站有哪些,微信小程序需要做网站吗?
网站制作员失业,怎样查看自己网站的注册者?
电视网站制作tvbox接口,云海电视怎样自定义添加电视源?
香港服务器部署网站为何提示未备案?
网站制作中优化长尾关键字挖掘的技巧,建一个视频网站需要多少钱?
建站之星后台密码遗忘?如何快速找回?
如何高效利用200m空间完成建站?
建站之星备案流程有哪些注意事项?
寿县云建站:智能SEO优化与多行业模板快速上线指南
网站制作哪家好,cc、.co、.cm哪个域名更适合做网站?
如何在Golang中处理模块冲突_解决依赖版本不兼容问题
小米网站链接制作教程,请问miui新增网页链接调用服务有什么用啊?
简历在线制作网站免费版,如何创建个人简历?
香港服务器建站指南:免备案优势与SEO优化技巧全解析
网站海报制作教学视频教程,有什么免费的高清可商用图片网站,用于海报设计?
整人网站在线制作软件,整蛊网站退不出去必须要打我是白痴才能出去?
猪八戒网站制作视频,开发一个猪八戒网站,大约需要多少?或者自己请程序员,需要什么程序员,多少程序员能完成?
香港服务器网站搭建教程-电商部署、配置优化与安全稳定指南
三星网站视频制作教程下载,三星w23网页如何全屏?
如何用PHP快速搭建高效网站?分步指南
宝塔建站无法访问?如何排查配置与端口问题?
胶州企业网站制作公司,青岛石头网络科技有限公司怎么样?
平台云上自主建站:模板化设计与智能工具打造高效网站
黑客如何利用漏洞与弱口令入侵网站服务器?
如何通过西部建站助手安装IIS服务器?
如何通过VPS搭建网站快速盈利?
,sp开头的版面叫什么?
Android自定义listview布局实现上拉加载下拉刷新功能
如何在七牛云存储上搭建网站并设置自定义域名?
Avalonia如何实现跨窗口通信 Avalonia窗口间数据传递
企业微网站怎么做,公司网站和公众号有什么区别?
建站主机类型有哪些?如何正确选型
如何快速搭建高效WAP手机网站吸引移动用户?
建站之星在线版空间:自助建站+智能模板一键生成方案
ui设计制作网站有哪些,手机UI设计网址吗?
建站之星图片链接生成指南:自助建站与智能设计教程
浅析上传头像示例及其注意事项
建站之星logo尺寸如何设置最合适?
*请认真填写需求信息,我们会在24小时内与您取得联系。