当我根据给定条件选中复选框时,我想要更改复选框按钮图标/图像。在这里我使用MSQS我想要如果ANSWER Correct检查然后更改文本框的图像,类似当答案检查错误然后也改变了图像,我已经保存图像在drawable请任何人告诉我它是如何可能的。 复选框选中一次后,不应再次重新检查或取消选中。提前谢谢谁会帮助我。
public void onCheckboxClicked(View view) {
switch(view.getId()) {
case R.id.chk_ans1ID:
ans_2.setChecked(false);
ans_3.setChecked(false);
ans_4.setChecked(false);
ans_2.setEnabled(false);
ans_3.setEnabled(false);
ans_4.setEnabled(false);
if(QuizActivity.op1.equalsIgnoreCase(QuizActivity.ans))
{
correct++;
} else if (QuizActivity.op2.equalsIgnoreCase(QuizActivity.ans)) {
ans_2.setChecked(true);
incorrect++;
}else if (QuizActivity.op3.equalsIgnoreCase(QuizActivity.ans)) {
ans_3.setChecked(true);
incorrect++;
}else if (QuizActivity.op4.equalsIgnoreCase(QuizActivity.ans)) {
ans_4.setChecked(true);
incorrect++;
}else{
Toast.makeText(QuizActivity.this, "No Answer Match!", Toast.LENGTH_LONG).show();
Toast.makeText(QuizActivity.this, "answer= "+ans, Toast.LENGTH_LONG).show();
}
break;
case R.id.chk_ans2ID:
ans_1.setChecked(false);
ans_3.setChecked(false);
ans_4.setChecked(false);
ans_1.setEnabled(false);
ans_3.setEnabled(false);
ans_4.setEnabled(false);
if(QuizActivity.op2.equalsIgnoreCase(QuizActivity.ans)){
correct++;
}else if (QuizActivity.op1.equalsIgnoreCase(QuizActivity.ans)) {
ans_1.setChecked(true);
incorrect++;
}else if (QuizActivity.op3.equalsIgnoreCase(QuizActivity.ans)) {
ans_3.setChecked(true);
incorrect++;
}else if (QuizActivity.op4.equalsIgnoreCase(QuizActivity.ans)) {
ans_4.setChecked(true);
incorrect++;
}else{
Toast.makeText(QuizActivity.this, "No Answer Match!", Toast.LENGTH_LONG).show();
Toast.makeText(QuizActivity.this, "answer= "+ans, Toast.LENGTH_LONG).show();
}
break;
}
}
这是我的xml复选框
<CheckBox
android:id="@+id/chk_ans4ID"
android:layout_width="match_parent"
android:layout_height="64dp"
android:button="@drawable/custom_checkbox"
android:onClick="onCheckboxClicked"
android:text="CheckBox" />
这是我的custom_checkbox.XML
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/checkbox_inactive"
android:state_checked="false"/>
<item android:drawable="@drawable/checkbox_active"
android:state_checked="true"/>
<item android:drawable="@drawable/checkbox_inactive"/>
</selector>
它在模拟器上对我很有用,但是当我在Android手机上使用此应用程序时,文本会覆盖复选框按钮图标,我该如何防止它?
答案 0 :(得分:3)
试试这个, 将此xml设为您的可绘制文件夹
<强> custom_checkbox.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/checkbox_inactive" android:state_checked="false"/>
<item android:drawable="@drawable/checkbox_active" android:state_checked="true"/>
<item android:drawable="@drawable/checkbox_inactive"/>
</selector>
并用它以编程方式添加drawable
yourcheckbox.setButtonDrawable(R.drawable.custom_checkbox);
并将其用于xml
android:button="@drawable/custom_checkbox"
答案 1 :(得分:0)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_focused="true"
android:drawable="@drawable/checkbox_on_background_focus_yellow" />
<item android:state_checked="false" android:state_focused="true"
android:drawable="@drawable/checkbox_off_background_focus_yellow" />
<item android:state_checked="false"
android:drawable="@drawable/checkbox_off_background" />
<item android:state_checked="true"
android:drawable="@drawable/checkbox_on_background" />
</selector>
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="new checkbox"
android:background="@drawable/checkbox_back"
android:button="@drawable/checkbox_icon" />