【问题标题】:Android checkbox remove the box keep the textAndroid复选框删除框保留文本
【发布时间】:2019-07-27 22:40:18
【问题描述】:

我正在使用根据复选框状态更改的自定义背景颜色,因此我的背景属性已被使用。大多数建议告诉我将背景设置为@null。我只想保留文本并删除左侧的框。

我已经可以改变背景颜色和文本颜色了。我只是想移除盒子。

<CheckBox
        android:text="CheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:background="@drawable/checkbox_selector" 
        android:textColor="@color/checkbox_text"/>

【问题讨论】:

  • 感谢您的所有回答。不过我找到了,只需要添加 android:button="@android:color/transparent"

标签: android kotlin checkbox


【解决方案1】:

您可以通过复选框的button 属性来更改the check drawable,如下所示:

<CheckBox
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:focusable="true"
            android:background="@drawable/checkbox_selector"
            android:button="@drawable/cb_show_hide_selector"
            android:textColor="@color/checkbox_text"
            android:text="this is a checkbox"/>

在drawable folder 中创建名为"cb_show_hide_selector.xml" 的文件并添加以下代码:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_checked="true"
          android:drawable="@drawable/ic_input_check" /> // for checked state
    <item android:state_pressed="true"
          android:drawable="@drawable/ic_input_check" /> // for checked state
   <item android:state_pressed="false"
          android:drawable="@android:color/transparent" /> // for unchecked state
</selector>

【讨论】:

    【解决方案2】:

    我为您提供两种不同的选择:

    1) 可以设置checkbox为android:clickable="false",在java代码中调用实例checkbox.set clickable = false

    2) 使用 set Visibility Gone 创建一个 textView,当您单击复选框时,使 checkBox.set visibility("GONE") 和 textView.set visibility("VISIBLE") 可以解决您的问题:D

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      示例代码复选框动态无方形按钮。

      private fun initCustomCheckbox(list : ArrayList<String>){
          for (i in list.indices) {
              val checkBox = AppCompatCheckBox(this)
              val params = FlowLayout.LayoutParams(
                  FlowLayout.LayoutParams.WRAP_CONTENT,
                  FlowLayout.LayoutParams.WRAP_CONTENT
              )
              params.setMargins(0, 0, 24, 24)
              checkBox.layoutParams = params
              checkBox.setPadding(24, 0, 24, 0)
              checkBox.id = i
              checkBox.tag = list[i]
              checkBox.text = list[i]
              checkBox.buttonDrawable = null
              checkBox.background = ContextCompat.getDrawable(this, R.drawable.selector_check_box_empty_corner)
              checkBox.setTextColor(ContextCompat.getColorStateList(this, R.drawable.selector_text_color_primary))
              checkBox.gravity = Gravity.CENTER
              checkBox.isClickable = true
              checkBox.setTextSize(TypedValue.COMPLEX_UNIT_PX, 40f)
              checkBox.setOnCheckedChangeListener { buttonView, isChecked ->
                  if (isChecked) {
                      buttonView.setPadding(0, 0, 24, 0)
                      buttonView.buttonDrawable = ContextCompat.getDrawable(this, R.drawable.ic_check)
                  } else {
                      checkBox.setPadding(24, 0, 24, 0)
                      buttonView.buttonDrawable = null
                  }
                  buttonView.layoutParams = params
              }
              flowLayout.addView(checkBox)
          }
      }
      

      Image Result above this code

      希望这段代码对你有所帮助^_^

      【讨论】:

      • 非常感谢您提供帮助的意图。您愿意为您的代码添加说明吗?
      猜你喜欢
      • 2014-06-11
      • 2011-12-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-10
      • 2020-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多