我的布局中有checkBox。我在代码中动态添加它。一切看起来都不错,但复选框不可点击。这是我的xml复选框:
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pref_calendar_choose_"
style="@style/PreferencesText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00ffffff"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:checked="false"
android:enabled="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="true"
android:gravity="center_vertical" />
这里是循环的活动,我正在添加复选框:
public class PreferencesActivity extends BaseActivity implements MenuHandler{
private static final String TAG = PreferencesActivity.class.getSimpleName();
private ActionBarDrawerToggle mDrawerToggle;
List<CalendarDB> mCalendarList;
@Bind(R.id.drawer_layout)
DrawerLayout mDrawerLayout;
@Bind(R.id.pref_calendar_choose)
public LinearLayout mPrefCalendarChoose;
@Bind(R.id.toolbar)
Toolbar mToolbar;
private View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox checkedTextView = (CheckBox) v;
CalendarDB.modify(mCalendarList.get(checkedTextView.getId()), !checkedTextView.isChecked());
BusStation.getInstance().post(new RefreshCalendarDataEvent());
}
};
@Override
protected void afterBind() {
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.open, R.string.close);
mDrawerLayout.addDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
mToolbar.setNavigationIcon(R.drawable.menu);
refreshView();
}
@Override
protected int getlayoutid() {
return R.layout.activity_preferences;
}
@Subscribe
public void refreshEvents(RefreshCalendarDataEvent event) {
refreshView();
}
private void refreshView() {
String userEmail = PreferencesManager.User.getEmail();
mCalendarList = CalendarDB.getAll(PreferencesManager.User.getEmail());
mPrefCalendarChoose.removeAllViews();
CheckBox checkBox;
for (int i = 0; i < mCalendarList.size(); i++) {
checkBox = (CheckBox) getLayoutInflater().inflate(R.layout.item_choose_calendar, null);
CalendarDB calendarDB = mCalendarList.get(i);
checkBox.setChecked(calendarDB.selected);
checkBox.setText(calendarDB.summary);
checkBox.setId(i);
checkBox.setOnClickListener(listener);
/* if(calendarDB.summary.contains(userEmail)) {
calendarDB.selected = true;
}
else {
calendarDB.selected = false;
}*/
Log.e(TAG, "refreshView: " + calendarDB.selected );
if (checkBox.getText() != "") ;
mPrefCalendarChoose.addView(checkBox);
}
}
答案 0 :(得分:0)
从XML布局中移出焦点
android:focusable="false" android:focusableInTouchMode="false"
,它将正常工作