【问题标题】:CheckBox setSelected not working when using android:drawableRight property使用 android:drawableRight 属性时 CheckBox setSelected 不起作用
【发布时间】:2015-07-30 15:29:58
【问题描述】:

xml布局内的我的复选框:

<CheckBox
                        android:id="@+id/cb1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:layout_marginTop="15dp"
                        android:button="@null"
                        android:drawableRight="?android:attr/listChoiceIndicatorMultiple"
                        android:paddingLeft="6dp"
                        android:paddingRight="5dp"
                        android:text="Trending Stores"
                        android:textColor="@color/black"
                        android:textSize="18sp" />

相关活动的onCreate()方法内部:

CheckBox cb1 = (CheckBox) findViewById(R.id.cb1);
cb1.setSelected(true);

当我运行应用程序时,这不会显示选中状态,而是复选框未选中。无法弄清楚如何解决这个问题。我还需要在右侧显示复选框,所以我使用属性 android:drawableRight。我也尝试过使用自定义可绘制 xml 文件,但我仍然遇到同样的问题。请帮忙。

【问题讨论】:

  • 我不明白你想把复选框放在你的屏幕上吗?您可以发布屏幕截图或其他内容来表明您想要什么吗?
  • cb1.setChecked(true);试试这个

标签: android checkbox


【解决方案1】:

啊,通常我们不会将setSelected 用于CheckBox

我认为你应该使用setChecked,除非你确切地知道你想用setSelected做什么。

【讨论】:

  • 也可以在xml中使用android:checked="true"
  • 是的,使用了错误的方法..后来想通了..但感谢您的回复。
猜你喜欢
  • 2019-10-09
  • 1970-01-01
  • 1970-01-01
  • 2012-07-04
  • 2012-04-21
  • 1970-01-01
  • 2016-11-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多