Android 中CheckBox多項(xiàng)選擇當(dāng)前的position信息提交的示例代碼
先給大家展示下效果圖:

廢話不多說(shuō)了,下面通過(guò)示例代碼給大家介紹checkbox 多項(xiàng)選擇當(dāng)前的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對(duì)象,這樣就省去了通過(guò)層層的findViewById去實(shí)例化我們需要的cb實(shí)例的步驟
MulAdapter.ViewHolder viewHolder = (MulAdapter.ViewHolder) view.getTag();
viewHolder.iv_select.toggle();// 把CheckBox的選中狀態(tài)改為當(dāng)前狀態(tài)的反,gridview確保是單一選中
MulAdapter.getIsSelected().put(position, viewHolder.iv_select.isChecked());////將CheckBox的選中狀況記錄下來(lái)
if (viewHolder.iv_select.isChecked() == true) {
Toast.makeText(ElseSelectCarinfosActivity.this,"對(duì)勾"+position,Toast.LENGTH_LONG).show();
} else {
Toast.makeText(ElseSelectCarinfosActivity.this,"取消"+position,Toast.LENGTH_LONG).show();
}
Toast.makeText(ElseSelectCarinfosActivity.this,"當(dāng)前"+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;//導(dǎo)入布局
private Context context;
//上下文
private ArrayList<String> list;
// 控制CheckBox選中情況
private static HashMap<Integer, Boolean> isSelected;
//導(dǎo)入布局
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的數(shù)據(jù)
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每顯示一行數(shù)據(jù),該函數(shù)就執(zhí)行一次
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
//當(dāng)?shù)谝淮渭虞dListView控件時(shí) convertView為空
convertView = inflater.inflate( R.layout.activity_elsecar_details, null);
//所以當(dāng)ListView控件沒(méi)有滑動(dòng)時(shí)都會(huì)執(zhí)行這條語(yǔ)句
holder = new ViewHolder();
holder.iv_select = (CheckBox) convertView.findViewById(R.id.iv_select);
convertView.setTag(holder);//為view設(shè)置標(biāo)簽
} else {
//取出holder
holder = (ViewHolder) convertView.getTag();
//the Object stored in this view as a tag
}
if (getIsSelected().get(position)!=null) {
// 根據(jù)isSelected來(lái)設(shè)置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="手續(xù)費(fèi)"
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的基類用來(lái)管理所有的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);
//創(chuàng)建公共的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);
//這個(gè)是讓子類去實(shí)現(xiàn)具體的view
View successView = getSuccessView();
//在添加之前要讓沒(méi)數(shù)據(jù)的時(shí)候子view也要填充滿
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
ll_activity_base.addView(successView, params);
//動(dòng)態(tài)添加view
setContentView(view);
//初始化頭部樣式的邏輯
intiHeadStyle(btn_headtitle_leftback, g_headtitle_title_textview);
//添加沉浸式狀態(tài)欄
addStatlan();
//判斷網(wǎng)絡(luò)是否打開(kāi)
if (NetUtil.isNetworkAvailable(MyApplication.context) == true) {//說(shuō)明網(wǎng)絡(luò)已經(jīng)打開(kāi)
//從服務(wù)器獲取數(shù)據(jù)
requestData();
} else {
Toast.makeText(MyApplication.context, "請(qǐng)連接網(wǎng)絡(luò)", 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();
}
});
//頭部樣式設(shè)置標(biāo)題讓子類去實(shí)現(xiàn)
String headTileinfo = setHeadStyleTitle();
g_headtitle_title_textview.setText(headTileinfo);
}
protected void finishActivity(){
finish();
}
/**
* 返回頭部字符串
*
* @return
*/
protected abstract String setHeadStyleTitle();
private void addStatlan() {
//判斷SDK版本是否大于等于19,大于就讓他顯示,小于就要隱藏,不然低版本會(huì)多出來(lái)一個(gè)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true);
tl_header_info.setVisibility(View.VISIBLE);
//還有設(shè)置View的高度,因?yàn)槊總€(gè)型號(hào)的手機(jī)狀態(tài)欄高度都不相同
} 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);
}
/**
* 初始化數(shù)據(jù)
*
* @return
*/
protected abstract void requestData();
/**
* 創(chuàng)建自己的view
*
* @return
*/
protected abstract View getSuccessView();
@Override
protected void onDestroy() {
super.onDestroy();
OkHttpUtils.getInstance().cancelTag(MyApplication.context);
}
/**
* 根據(jù)EditText所在坐標(biāo)和用戶點(diǎn)擊的坐標(biāo)相對(duì)比,來(lái)判斷是否隱藏鍵盤(pán),因?yàn)楫?dāng)用戶點(diǎn)擊EditText時(shí)沒(méi)必要隱藏
*
* @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) {
// 點(diǎn)擊EditText的事件,忽略它。
return false;
} else {
return true;
}
}
// 如果焦點(diǎn)不是EditText則忽略,這個(gè)發(fā)生在視圖剛繪制完,第一個(gè)焦點(diǎn)不在EditView上,和用戶用軌跡球選擇其他的焦點(diǎn)
return false;
}
/**
* 多種隱藏軟件盤(pán)方法的其中一種
*
* @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) {
// 獲得當(dāng)前得到焦點(diǎn)的View,一般情況下就是EditText(特殊情況就是軌跡求或者實(shí)體案件會(huì)移動(dòng)焦點(diǎn))
View v = getCurrentFocus();
if (isShouldHideInput(v, ev)) {
hideSoftInput(v.getWindowToken());
}
}
return super.dispatchTouchEvent(ev);
}
}
以上所述是小編給大家介紹的Android 中CheckBox多項(xiàng)選擇當(dāng)前的position信息提交的示例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Android CheckBox中設(shè)置padding無(wú)效解決辦法
- Android開(kāi)發(fā)之CheckBox的簡(jiǎn)單使用與監(jiān)聽(tīng)功能示例
- Android 中CheckBox的isChecked的使用實(shí)例詳解
- Android開(kāi)發(fā)手冊(cè)自定義Switch開(kāi)關(guān)按鈕控件
- Android開(kāi)關(guān)控件Switch的使用案例
- Android 自定義Switch開(kāi)關(guān)按鈕的樣式實(shí)例詳解
- Android UI控件Switch的使用方法
- Android單選按鈕RadioButton的使用方法
- Android復(fù)選框CheckBox與開(kāi)關(guān)按鈕Switch及單選按鈕RadioButton使用示例詳解
相關(guān)文章
Android編程實(shí)現(xiàn)長(zhǎng)按彈出選項(xiàng)框View進(jìn)行操作的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)長(zhǎng)按彈出選項(xiàng)框View進(jìn)行操作的方法,結(jié)合實(shí)例形式分析了Android事件響應(yīng)及彈窗的功能、布局相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
Android小部件Widget開(kāi)發(fā)過(guò)程中的坑和問(wèn)題小結(jié)
這篇文章主要介紹了Android小部件Widget開(kāi)發(fā)過(guò)程中的坑和問(wèn)題小結(jié),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
Android IPC進(jìn)程間通信詳解最新AndroidStudio的AIDL操作)
這篇文章主要介紹了Android IPC進(jìn)程間通信的相關(guān)資料,需要的朋友可以參考下2016-09-09
Android編程雙重單選對(duì)話框布局實(shí)現(xiàn)與事件監(jiān)聽(tīng)方法示例
這篇文章主要介紹了Android編程雙重單選對(duì)話框布局實(shí)現(xiàn)與事件監(jiān)聽(tīng)方法,涉及Android雙重單選對(duì)話框的界面布局與事件監(jiān)聽(tīng)、響應(yīng)等相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
Android App開(kāi)發(fā)中使用RecyclerView替代ListView的實(shí)踐
RecyclerView是Android L即5.0版本以來(lái)新加入的一個(gè)組件,主要用來(lái)實(shí)現(xiàn)item的瀑布式排列,因而被人們廣泛認(rèn)為用來(lái)替代ListView,這里我們就來(lái)看一下Android App開(kāi)發(fā)中使用RecyclerView替代ListView的實(shí)踐:2016-06-06
Android實(shí)現(xiàn)局部圖片滑動(dòng)指引效果示例
現(xiàn)在滑動(dòng)效果用的比較多,尤其是在手機(jī)端上面,本文介紹了Android實(shí)現(xiàn)局部圖片滑動(dòng)指引效果示例,現(xiàn)在就分享給大家,也給大家做個(gè)參考。2016-10-10
Android錄音--AudioRecord、MediaRecorder的使用
本篇文章主要介紹了Android錄音--AudioRecord、MediaRecorder的使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
Android開(kāi)發(fā)軟鍵盤(pán)遮擋登陸按鈕的完美解決方案
在應(yīng)用登陸頁(yè)面我們需要填寫(xiě)用戶名和密碼。當(dāng)填寫(xiě)這些信息的時(shí)候,軟鍵盤(pán)會(huì)遮擋登陸按鈕,這使得用戶體驗(yàn)較差。今天小編給大家分享本教程給大家介紹解決android軟鍵盤(pán)遮擋登陸按鈕的方法,感興趣的朋友一起學(xué)習(xí)吧2016-10-10
Android開(kāi)發(fā)使用自定義View將圓角矩形繪制在Canvas上的方法
這篇文章主要介紹了Android開(kāi)發(fā)使用自定義View將圓角矩形繪制在Canvas上的方法,結(jié)合實(shí)例形式分析了Android自定義view繪制圓角矩形的相關(guān)方法與使用技巧,需要的朋友可以參考下2017-10-10

