且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ListView控件+复选框=?

更新时间:2022-06-03 03:21:47

答案很简单!有一个调用的组件 CheckedTextView 这是一个的TextView 复选框$的组合C $ C>。 该组件可以简化你的逻辑,让你***地做修改你想要的任何东西的清单的 OnItemClickListener

The answer is quite simple! There's a component called CheckedTextView which is a combination of a TextView and a CheckBox. This component might simplify your logic, leaving you free to do modify anything you want in your list's OnItemClickListener!

我为你写了一个例子:

    public class CheckBoxListView extends ListActivity implements OnItemClickListener{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Mock data
        String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
                "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
                "Linux", "OS/2" };

        //android's simple_list_item_multiple_choice is a CheckedTextView
        //try creating your own later ;)
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_multiple_choice, values);

        getListView().setOnItemClickListener(this);

        setListAdapter(adapter);

    }

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

        CheckedTextView item = (CheckedTextView) arg1;

        //The change color logic is here!
        if(item.isChecked()) {
            item.setTextColor(Color.BLACK);
            item.setChecked(false);
        }
        else {
            item.setTextColor(Color.RED);
            item.setChecked(true);
        }

    }

}