我在網上搜索了近2個小時,但找不到任何對我有用的東西。如何以編程方式檢查Android中的複選框?
簡單,我有一個android應用程序,與listview,其中包括複選框。
現在,當我點擊一個按鈕,我只想要一個複選框被檢查(並且這在屏幕上是可見的) 無論我迄今爲止嘗試了什麼,複選框都不會被檢查。
這就是我的代碼:
public class MainActivity extends Activity {
SparseArray<Group> groups = new SparseArray<Group>();
List<String> checkedMedia = new ArrayList<String>();
ExpandableListView listView;
MyExpandableListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createData();
listView = (ExpandableListView) findViewById(R.id.listView);
adapter = new MyExpandableListAdapter(this,
groups);
listView.setAdapter(adapter);
listView.expandGroup(0);
}
public void onCheckboxClicked(View view) {
CheckBox checkBox = (CheckBox) view;
boolean checked = checkBox.isChecked();
View chBoxParent = (View) checkBox.getParent();
TextView textView = (TextView) chBoxParent.findViewById(R.id.textView1);
String medium = textView.getText().toString();
if (checked) {
Toast.makeText(this, "checked: " + medium, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "unchecked: " + medium, Toast.LENGTH_SHORT).show();
}
}
public void onButtonClicked(View view) {
View view0 = adapter.getChildView(0, 0, false, null, null);
CheckBox cb0 = (CheckBox) view0.findViewById(R.id.checkBox);
cb0.setChecked(true);
cb0.setWillNotDraw(false);
cb0.invalidate();
Toast.makeText(this, "button clicked", Toast.LENGTH_SHORT).show();
}
public void createData() {
Group group = new Group("Test ");
for (int i = 0; i < 5; i++) {
group.children.add("Sub Item" + i);
}
groups.append(0, group);
}
}
你需要設置CompoundButton.OnCheckedChangeListener的複選框,並沒有onClickListener –
是否要將複選框設置爲選中狀態當您點擊按鈕時,什麼是所有項目在列表視圖中都可見? – Vishwa