CheckedTextView和listview配合使用定作checkbox

CustomizeCheckBoxActivity.java
package com.mw.app.view.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.mw.app.R;

import java.util.ArrayList;
import java.util.List;

public class CustomizeCheckBoxActivity extends AppCompatActivity {
    private CheckedTextView checkedTextView;
    ArrayAdapter adapter = null;
    ListView listView = null;
    ArrayList dataList = null;
    List checkedList;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.customize_checkbox_layout);
        checkedList = new ArrayList<>();
        //单独一个checkBox
        checkedTextView = findViewById(R.id.customize_checkbox_item_layout_text1);
        checkedTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("mw","自定义当前状态是:"+(checkedTextView.isChecked()?"选中":"未选中"));
                checkedTextView.setChecked(!checkedTextView.isChecked());

            }
        });





        //模拟数据
        ArrayList dataList = new ArrayList<>();
        for (int i = 0; i < 13; i++) {
            dataList.add("选择数据"+i);
        }
        //初始化ListView
        listView = (ListView) findViewById(R.id.customize_checkbox_layout_listview);
        listView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);//设置多模式
        //ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_multiple_choice); 系统样式
        adapter = new ArrayAdapter(this,R.layout.customize_checkbox_item_layout);
        //将模拟数据添加到adapter适配器中
        adapter.addAll(dataList);
        listView.setAdapter(adapter);

        //点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                CheckedTextView c = (CheckedTextView)view;
                Log.i("mw","被执行了onItemClick,当前状态是:"+c.isChecked());
                checkedList.add(c);
            }
        });

    }

    public void getAllSelectVal(View view){
        Log.i("mw","11111111");
       // Toast.makeText(getApplicationContext(),"被选中N条",Toast.LENGTH_SHORT);

        int count = 0;
        for (int i=0;i

customize_checkbox_layout.xml



    

 

border_style.xml



    
   
    
    
    
    

 

customize_checkbox_item_layout_text_bg.xml



    
    
    
    

 



图标

CheckedTextView和listview配合使用定作checkbox_第1张图片CheckedTextView和listview配合使用定作checkbox_第2张图片CheckedTextView和listview配合使用定作checkbox_第3张图片

CheckedTextView和listview配合使用定作checkbox_第4张图片

你可能感兴趣的:(Android,checkbox,listview,ui)