Android 에서 CheckBox 는 현재 position 정보 가 제출 한 예제 코드 를 여러 개 선택 합 니 다.

먼저 효과 도 를 보 여 드 리 겠 습 니 다.

쓸데없는 말 은 그만 하 겠 습 니 다.다음은 예제 코드 를 통 해 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 정 보 를 선택 하여 제출 한 예제 코드 입 니 다.여러분 에 게 도움 이 되 기 를 바 랍 니 다.궁금 한 점 이 있 으 시 면 저 에 게 메 시 지 를 남 겨 주세요.소 편 은 제때에 답 해 드 리 겠 습 니 다.여기 서도 저희 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!

좋은 웹페이지 즐겨찾기