【问题标题】:OnClickListener not fired on ConstraintLayoutOnClickListener 未在 ConstraintLayout 上触发
【发布时间】:2018-07-21 17:58:45
【问题描述】:

在我的 android 应用程序项目中,我必须在背景上制作一个带有 ProgressBar 的按钮和两个 TextView。

我第一次尝试是这样的:

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

看起来不错,但是当我尝试添加 OnClickListener 时,它无法正常工作。

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

OnClickListener 没有被触发,我不知道为什么。这可能很简单,但我不明白为什么。

提前致谢。

【问题讨论】:

标签: android kotlin onclicklistener android-constraintlayout


【解决方案1】:

删除

android:clickable="false"

From Docs

定义此视图是否响应点击事件

并将keyboard_touch_1.setOnClickListener {...} 移动到onResume 的末尾,因为 DataBindingUtil.setContentView 将重置之前设置的布局 (setContentView),因此您将拥有一个带有新视图的新布局。

注意:您正在使用数据绑定以及正常初始化setContentView(R.layout.activity_game) 技术,因此最佳方法是使用

Event Handling via data binding

【讨论】:

    【解决方案2】:

    由于我的代表率低,我无法发表评论,我将不得不发布答案。

    根据您通过 GitHub 链接提供的代码,您实际上是在尝试设置内容视图两次。

    首先,您在 onCreate() 方法中调用 setContentView(R.layout.activity_game),然后在 onResume() 方法中调用 DataBindingUtil.setContentView(this, R.layout.activity_game)

    请尝试删除setContentView(R.layout.activity_game),然后将您的数据绑定设置代码移动到您的onCreate 方法()。然后,您可以通过“绑定”对象作为属性访问约束布局视图。

    例如:

    override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
            viewModel = GameViewModel()
            val binding: ActivityGameBinding = DataBindingUtil.setContentView(this, R.layout.activity_game)
            binding.viewModel = viewModel
    
            binding.keyboardTouch1.setOnClickListener {
                Toast.makeText(this, "IT WORKS !!!", Toast.LENGTH_SHORT).show()
            }
        }
    
        override fun onResume() {
            super.onResume()
            viewModel.startUpdate()
        }
    
        override fun onPause() {
            super.onPause()
            viewModel.stopUpdate()
        }
    

    【讨论】:

      【解决方案3】:

      也许你的布局包含带有 id 的标签,这个 id 会覆盖原点 id。

      【讨论】:

        猜你喜欢
        • 2021-06-09
        • 1970-01-01
        • 2023-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多