【发布时间】:2014-05-23 02:44:05
【问题描述】:
我在我的 java 代码中为 xml 中的按钮设置了一个切换开关。代码如下:
@Override
public void onClick(final View view) {
switch (view.getId()) {
case R.id.selectable_text:
if(view instanceof CheckedTextView){
CategoryCheckableRow checkableRow = ((CheckedTextView)view).getCategoryCheckableRow();
toggleCategoryRow(view, checkableRow);
Log.d("newsdash","I am toggling in dash onclick");
if (!mCategoriesSet.add(checkableRow)) {
mCategoriesSet.remove(checkableRow);
}
mDoneButton.setEnabled(!mCategoriesSet.isEmpty());
}
return;
case R.id.button_done:
sendCategoriesToActivity();
((DashboardManageNewsCategoriesActivity) getActivity()).updateCategoriesMap(mCategoriesSet);
break;
default:
}
}
当我的当前复选框被选中时,我计划取消选中所有其他“检查”。假设我有复选框 1、2 和 3,如果我选中 1,我希望取消选中 2 和 3,依此类推。有没有办法用上面的代码实现这一点?说(如果光标(或可能视图)。getcursor 或当前位置)=当前复选框已选中){取消选中除当前复选框之外的所有其他复选框}?
这里也是切换行:
private void toggleCategoryRow(final View view, final CategoryCheckableRow checkableRow) {
final CheckedTextView textView = (CheckedTextView) view;
textView.toggle();
checkableRow.setSelected(textView.isChecked());
}
这里是对应的xml文件(供参考):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/altercolor2"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/altercolor2"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/button_top_news"
android:layout_width="match_parent"
android:layout_height="@dimen/manage_news_btn_ht"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:background="@drawable/manage_market_category_btnbg"
android:gravity="left|center_vertical"
android:paddingLeft="10dp"
android:drawablePadding="10dp"
android:text="@string/button_top_news"
android:textColor="#cccccc"
android:textSize="@dimen/title"
android:drawableLeft="@drawable/arrow_expand_collapse"/>
</RelativeLayout>
</linearlayout>
</,... etc
【问题讨论】:
-
您是否有理由不使用
RadioGroup,因为这是它的预期功能,而不是Checkbox? -
我不使用radiogroup的原因是我有不同的类别和自己的子类别,所以我发现很难维护,除了我以前从未使用过它们
-
现在是开始的好时机。当用户看到复选框时,通常他们希望能够选中多个复选框。当他们看到一个广播组时,他们通常知道他们只能选择一个,选择一个会取消选中其他的。它确实会让您的生活更轻松,为您的用户带来更愉快的体验
-
你能举一个关于我的代码的例子吗?我已经发布了我的 xml
-
@Emmanuel 那张照片比比伯还老哈哈,我20岁左右,现在27岁,他抄袭了我的风格哈哈
标签: android android-fragments android-button android-cursor android-checkbox