ListView控件+复选框=? [英] ListView + CheckBox =?
问题描述
可能重复:
<一href="http://stackoverflow.com/questions/8841283/gmail-like-listview-with-checkboxes-and-using-the-actionbar">Gmail-like ListView控件使用复选框(并用动作条)
我需要:
ListView控件与
ListView with
items { CheckBox, then TextView } ;
在$上的复选框p $ PSS,项目应该改变它的颜色。
when you press on the CheckBox , item should change it's color.
我怎样才能得到它?
P.S。
在换句话说,我需要有一个复选框的ListView像Gmail应用程序
In other words I need a ListView with CheckBoxes like in Gmail app
推荐答案
答案很简单!有一个调用的组件 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);
}
}
}
这篇关于ListView控件+复选框=?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!