【问题标题】:Android setclickable for custom view not work自定义视图的Android setclickable不起作用
【发布时间】:2020-01-18 08:46:49
【问题描述】:

我需要创建一个自定义视图来制作仿冒按钮,而且该按钮的启用与否取决于另一个复选框。

我希望该按钮在未选中复选框时保持不可点击,但它似乎无法按预期工作。

checkbox的控制逻辑在MainActivity:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.use_so_botton)
    val cusBtn = findViewById<CusButton>(R.id.cus_btn_in_use)
    val chkBox = findViewById<CheckBox>(R.id.check_box_in_use)
    chkBox.setOnCheckedChangeListener{
        _, isChecked ->
            cusBtn.isClickable = isChecked

    }

    cusBtn.setOnClickListener {
        Toast.makeText(this@MainActivity, "Gotta you, custom view", Toast.LENGTH_LONG).show()
    }

MainActivity的布局很简单,将ConstraintLayout命名为use_so_botton.xml

<kot.bignerd.linearlay101.CusButton
        android:id="@+id/cus_btn_in_use"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="66dp"
        android:layout_marginEnd="66dp"
        android:layout_marginStart="66dp"
        android:clickable="false"
        android:focusable="false"
        app:image="@drawable/sun"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:text="Yo haha" />

<CheckBox
        android:id="@+id/check_box_in_use"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Use!"
        app:layout_constraintTop_toBottomOf="@+id/cus_btn_in_use"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginTop="66dp"
        android:layout_marginEnd="66dp"
        android:layout_marginStart="66dp"
        tools:layout_editor_absoluteX="68dp"
        tools:layout_editor_absoluteY="117dp" />

布局内的&lt;kot.bignerd.linearlay101.CusButton标签是自定义视图的ConstraintLayout xml本身:

<ImageView
        android:id="@+id/ImageView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:background="@drawable/budhha"
        tools:src="@color/colorPrimary"
        >
</ImageView>

<TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        tools:text="Caption of the Image"
        android:text="ButtText"
        android:gravity="center"
        app:layout_constraintStart_toEndOf="@+id/ImageView01"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:textColor="#000000">
</TextView>

还有CusButton.kt获取参数:

class CusButton(context: Context, attrs: AttributeSet) : ConstraintLayout(context, attrs) {
    init {
        View.inflate(context, R.layout.so_button, this)

        val imageView: ImageView = findViewById(R.id.ImageView01)
        val textView: TextView = findViewById(R.id.TextView01)

        val attributes = context.obtainStyledAttributes(attrs, R.styleable.CusButton)
        imageView.setImageDrawable(attributes.getDrawable(R.styleable.CusButton_image))
        textView.text = attributes.getString(R.styleable.CusButton_text)
        attributes.recycle()
    }
}

现在,即使未选中复选框,我们仍然可以单击按钮并敬酒。

我想知道如何在选中复选框之前禁用仿冒按钮。

【问题讨论】:

  • 您还需要将 'setEnabled' 设置为 true。 view.setEnabled(true);
  • @Shahin 谢谢,请将其作为单行答案回复,在这种情况下,我可以接受它作为答案。
  • 欢迎您。祝你一切顺利

标签: android


【解决方案1】:

这个问题的答案很简单view.setEnabled(true);

说明: 用于在任何视图上启用 Clicks 和 Touch 事件

启用点击侦听器响应

myView.setClickable(true);

<MyView
    ...
    android:clickable="true"
    android:focusable="true">

这些属性将启用“ClickLlistener”响应。例如,按钮已经设置为按下,不需要任何“setEnabled”

但自定义视图或只是未设置为单击、触摸或按下的视图,我们需要在代码中将“setEnabled”设置为 true

myView.setEnabled(true);

另外,当您编写view.setOnClickListener 时,视图自动为setEnabled(true),但点击不起作用,因为它需要setClickable(true) 开始监听点击事件的响应

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    • 2020-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多