在我的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未被触发,我不知道为什么。它可能很简单,但我不明白为什么。
提前致谢。
答案 0 :(得分:5)
删除
android:clickable="false"
定义此视图是否对点击事件做出反应
并在keyboard_touch_1.setOnClickListener {...}
的末尾移动onResume
因为
DataBindingUtil.setContentView
会重置先前设置的布局(setContentView
),因此您将拥有包含新视图的新布局。
注意:您正在使用数据绑定以及常规初始化setContentView(R.layout.activity_game)
技术,因此最佳方式是使用
答案 1 :(得分:1)
由于我的低代表我无法发表评论,我将不得不发表回答。
根据您通过GitHub链接提供的代码,您实际上是在尝试将内容视图设置两次。
首先,您在onCreate()方法中调用setContentView(R.layout.activity_game)
,然后在onResume()方法中调用DataBindingUtil.setContentView(this, R.layout.activity_game)
。
请尝试删除setContentView(R.layout.activity_game)
,然后将数据绑定设置代码移至onCreate方法()。然后,您可以通过&#34;绑定&#34;访问您的约束布局视图。作为财产的对象。
例如:
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()
}
答案 2 :(得分:0)
也许您的布局包含带有 id 的标签,此 id 会覆盖原始 id。