【问题标题】:How to Change checkBox button icon in Android?如何更改 Android 中的复选框按钮图标?
【发布时间】:2016-09-25 18:38:48
【问题描述】:

当我根据给定条件选中复选框时,我想更改 CheckBox 按钮图标/图像。在这里,我正在使用 MSQS,如果 ANSWER Correct 检查然后更改文本框的图像,类似地,当 Answer 检查为 false 然后也更改图像时,我已将图像保存在可绘制中,请任何人告诉我这是怎么可能的。 当 Checkbox 选中一次时,不应再次选中或取消选中。提前谢谢谁会帮助我。

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 手机上使用这个应用程序时,复选框按钮图标上的文本覆盖了我如何防止它?

【问题讨论】:

标签: android xml checkbox


【解决方案1】:

试试这个, 将此 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>

并将其用于以编程方式添加可绘制对象

yourcheckbox.setButtonDrawable(R.drawable.custom_checkbox);

并将其用于 xml

android:button="@drawable/custom_checkbox"

【讨论】:

  • 当 mcsqs 回答不是 mtach 时如何管理交叉图标图像?它应该是交叉图标。
  • 它在模拟器上对我来说非常有用,但是当我在 Android 手机上使用这个应用程序时,复选框按钮图标上的文本覆盖了我如何防止它?
  • @saim 尝试在您的复选框上添加一些 padding
【解决方案2】:

首先在drawable文件夹中创建checkbox_icon.xml

<?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>

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

<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" />

【讨论】:

  • 它在模拟器上对我来说非常有用,但是当我在 Android 手机上使用这个应用程序时,复选框按钮图标上的文本会覆盖我如何防止它?请任何人帮助我。
猜你喜欢
  • 2012-04-26
  • 2014-05-07
  • 1970-01-01
  • 2016-08-07
  • 2013-04-19
  • 1970-01-01
  • 2015-12-06
  • 2011-04-04
  • 2018-01-31
相关资源
最近更新 更多