有一些我自己无法解决的问题。我目前正在为我的训练制作一个简单的android应用,该应用利用了多个复选框。
我想做的是如标题所说的,当条件为setEnabled(true)时,更改复选框文本的颜色。我已经搜索过Google,但是找不到解决方案。我在下面添加了我正在使用的示例代码。因此,我是菜鸟,专业人士的帮助对我来说意义非凡。
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
buttonView.setTextColor(Color.GREEN);
buttonClicked.add(buttonView.getText()
.toString());
buttonView.setTextSize(18);
count += 1;
Log.d("TAG","is checked = "+isChecked);
if (count >= 7) {
for (int i = 0; i < checkbox.length; i++) {
String item = Integer.toString(i+1);
if (buttonClicked.indexOf(item)>-1) {
checkbox[i].setEnabled(true);
Log.d("TAG",i+" = 2is checked = "+isChecked);
// buttonView.setTextColor(Color.YELLOW);
} else {
Log.d("TAG",i+" = 3is checked = "+isChecked);
checkbox[i].setEnabled(false);
// buttonView.setTextColor(Color.RED);
}
}
}
} else {
if (count <= 7) {
for (int i = 0; i < checkbox.length; i++) {
checkbox[i].setEnabled(true);
// buttonView.setTextColor(Color.BLACK);
}
}
count--;
buttonClicked.remove(buttonView.getText()
.toString());
buttonView.setTextSize(15);
}
创建选择器xml并将其放在/res/color/text_my_checked.xml中
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#00ff00"/>
<item android:color="#000000"/> <!-- anything else -->
</selector>
并在这样的布局文件中使用它
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check"
android:textColor="@color/text_my_checked"
android:checked="true" />
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句