如何在Android中更改复选框按钮图标?

如何在Android中更改复选框按钮图标?,android,xml,checkbox,Android,Xml,Checkbox,当我根据给定条件选中复选框时,我想更改复选框按钮图标/图像。在这里,我使用的MSQ我想如果答案正确检查,然后改变文本框的图像,同样当答案检查错误,然后也改变了图像,我已经保存在绘图图像请任何人告诉我这是如何可能的。 如果复选框选中一次,则不应再次检查或取消选中该复选框。有谁会帮助我呢 public void onCheckboxClicked(View view) { switch(view.getId()) { case R.id.chk_

当我根据给定条件选中复选框时,我想更改复选框按钮图标/图像。在这里,我使用的MSQ我想如果答案正确检查,然后改变文本框的图像,同样当答案检查错误,然后也改变了图像,我已经保存在绘图图像请任何人告诉我这是如何可能的。 如果复选框选中一次,则不应再次检查或取消选中该复选框。有谁会帮助我呢

     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>


它在emulator上对我来说很好,但当我在Android手机上使用这个应用程序时,复选框按钮图标上的文本覆盖怎么能阻止它呢

试试这个, 将此xml设置为可绘制文件夹

自定义复选框.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>
并将其用于xml

android:button="@drawable/custom_checkbox"
试试这个, 将此xml设置为可绘制文件夹

自定义复选框.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>
并将其用于xml

android:button="@drawable/custom_checkbox"
首先在drawable文件夹中创建复选框_icon.xml

然后像这样在布局xml中使用它

首先在可绘图文件夹中创建复选框\u icon.xml

然后像这样在布局xml中使用它


同样的问题和回答非常好同样的问题和回答非常好mcsqs回答非mtach时如何管理交叉图标图像?它应该是交叉图标。它在emulator上对我来说很好,但当我在Android mobile上使用此应用程序时,然后在复选框按钮图标上文本覆盖我如何防止它?@saim尝试在复选框上添加一些
填充
当mcsqs回答非mtach时如何管理交叉图标图像?它应该是交叉图标。emulator对我很有用,但当我在Android mobile上使用此应用时,然后在复选框按钮图标上文本覆盖我如何防止它?@saim尝试在你的复选框上添加一些
填充
,emulator对我很有用,但当我在安卓手机上使用这个应用程序时,复选框按钮图标上的文本覆盖怎么防止呢?请任何人帮助我。这对我来说在emulator上很好,但当我在Android手机上使用这个应用程序时,然后文本覆盖复选框按钮图标,我如何才能阻止它?请任何人帮帮我。
<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" />