在我的android应用程序项目中,我必须在后台制作一个带有进度条和两个文本视图的按钮.

I make a first attempt like this:

    <android.support.constraint.ConstraintLayout
        android:id="@+id/keyboard_touch_1"
        android:layout_width="60dp"
        android:layout_height="85dp"
        android:layout_marginBottom="150dp"
        android:layout_marginStart="10dp"
        android:focusable="true"
        android:clickable="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <ProgressBar
            android:id="@+id/keyboard_touch_1_progress_bar"
            style="@android:style/Widget.ProgressBar.Horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:indeterminate="false"
            android:max="100"
            android:progress="50"
            android:progressDrawable="@drawable/button_progress_bar_default"
            android:clickable="false"
            android:focusable="false" />

        <TextView
            android:id="@+id/keyboard_touch_1_score"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_marginEnd="8dp"
            android:layout_marginTop="8dp"
            android:text="2"
            android:textColor="@color/colorAccent"
            android:textSize="11dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="3"
            android:clickable="false"
            android:focusable="false" />

        <TextView
            android:id="@+id/keyboard_touch_1_letter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_marginTop="13dp"
            android:text="A"
            android:textColor="@color/colorAccent"
            android:textSize="45dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:clickable="false"
            android:focusable="false" />

    </android.support.constraint.ConstraintLayout>

It looks good but when i attempt to add an OnClickListener it doesn't work correctly.

keyboard_touch_1.setOnClickListener {
    Toast.makeText(this, "IT WORKS !!!", Toast.LENGTH_SHORT).show()
}

The OnClickListener is not fired and i have no idea of why. It's probably very simple but i doesn't understand why.

Thanks in advance.

推荐答案

Remove

android:clickable="false"

From Docs

定义此视图是否对单击事件做出react

and move keyboard_touch_1.setOnClickListener {...} at the end of onResume because DataBindingUtil.setContentView will reset the previously set layout (setContentView) therefore you have a new layout with new views.

注意:您正在使用数据绑定和正常的初始化技术,因此最佳的方法是使用

Event Handling via data binding

Kotlin相关问答推荐

来自SnapshotFlow的单元测试StateFlow仅发出initialValue

Spring Boot Bean验证器未触发

我可以更改方法中泛型类的类型参数边界吗?

如何让 LocalDateTime.parse 拒绝 24:00 时间

合并状态流

如何注入返回通用列表的转换器?

如何在数据类中删除空格 (String)?

如何获取@JsonProperty 名称列表?

如何在 Spring Boot 3 中为内部类提供运行时提示

在 kotlin 中重载函数时,我在一些非常基本的代码上不断收到类型不匹配

如何在 Kotlin for Android 上使用setTextColor(hexaValue),

具有多个不同类型来源的 LiveData

AIDL 中的 Parcelize 注释:Incompatible types: Object cannot be converted to MyCustomObject

为什么 IntelliJ Idea 无法识别我的 Spek 测试?

Kotlin 中的部分类委托

如何退出 Kotlinc 命令行编译器

有没有办法在数据类构建时转换属性的值?

如何在不绑定ViewModel(MVVM)中的UI的情况下使用android导航?

在 Kotlin 中声明 Byte 会出现编译时错误The integer literal does not conform to the expected type Byte

为什么 Kotlin 会收到这样的 UndeclaredThrowableException 而不是 ParseException?