如何解决android中的多个按钮处理
我有六个按钮
单击其中一个按钮时,请制作动画。因此,我将为其赋予动画效果(单击时会很大)。
因此,如果我给所有按钮一个动画,则单击一个按钮时必须将另一个按钮放回原处。大概只需激活一个按钮 一度 。 代码:
btn1 = findviewby<Button>(R.id.btn1)
btn2 = findviewby<Button>(R.id.btn2)
btn3 = findviewby<Button>(R.id.btn3)
btn4 = findviewby<Button>(R.id.btn4)
btn5 = findviewby<Button>(R.id.btn5)
btn6 = findviewby<Button>(R.id.btn6)
btn1.setonClickListener {
btn1.animate()
.scaleXBy(0.1f)
.scaleYBy(0.1f)
.duration = 200
// and if the other btn's (btn2,3,4,5,6) is get animation already go back on there position
}
如果我对每个btn都执行以上代码,那么所有这些动画都会生效,这是不正确的。
非常感谢
解决方法
解决方案:在一组按钮中,一次只能将一个按钮转换为给定的x,如果已经转换,则y的其余按钮将重置为原始位置。
activity_buttons.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ButtonsActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginStart="149dp"
android:layout_marginTop="87dp"
android:text="Button"
app:layout_constraintBottom_toTopOf="@+id/button2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginStart="2dp"
android:text="Button"
app:layout_constraintBottom_toTopOf="@+id/button3"
app:layout_constraintStart_toStartOf="@+id/button"
app:layout_constraintTop_toBottomOf="@+id/button" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginStart="2dp"
android:text="Button"
app:layout_constraintBottom_toTopOf="@+id/button4"
app:layout_constraintStart_toStartOf="@+id/button2"
app:layout_constraintTop_toBottomOf="@+id/button2" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginEnd="2dp"
android:text="Button"
app:layout_constraintBottom_toTopOf="@+id/button5"
app:layout_constraintEnd_toEndOf="@+id/button5"
app:layout_constraintTop_toBottomOf="@+id/button3" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginStart="17dp"
android:text="Button"
app:layout_constraintStart_toStartOf="@+id/button3"
app:layout_constraintTop_toBottomOf="@+id/button4" />
</androidx.constraintlayout.widget.ConstraintLayout>
ButtonsActivity.kt
class ButtonsActivity : AppCompatActivity() {
private val buttonList = mutableListOf<Button>()
var activatedButtonIndex = -1
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_buttons)
val button1 = findViewById<Button>(R.id.button)
val button2 = findViewById<Button>(R.id.button2)
val button3 = findViewById<Button>(R.id.button3)
val button4 = findViewById<Button>(R.id.button4)
val button5 = findViewById<Button>(R.id.button5)
button1.setOnClickListener(listener)
button2.setOnClickListener(listener)
button3.setOnClickListener(listener)
button4.setOnClickListener(listener)
button5.setOnClickListener(listener)
//set tags for dynamically identify the object in foreach instead of resource id
button1.tag = 0
button2.tag = 1
button3.tag = 2
button4.tag = 3
button5.tag = 4
buttonList.add(button1)
buttonList.add(button2)
buttonList.add(button3)
buttonList.add(button4)
buttonList.add(button5)
}
private val listener = View.OnClickListener { view ->
val tag: Int = view.tag as Int
view?.let {
if (activatedButtonIndex == tag) {
return@let
}
startTranslateAnim(it)
if (activatedButtonIndex != tag) {
resetTranslationAnim(activatedButtonIndex)
}
activatedButtonIndex = tag
}
}
private fun startTranslateAnim(it: View) {
it.animate()
.scaleXBy(0.1f)
.scaleYBy(0.1f)
.duration = 200
}
private fun resetTranslationAnim(previousSelectedIndex: Int) {
buttonList?.let {
it.forEachIndexed { index,button ->
if (index == previousSelectedIndex)
button.animate().scaleXBy(-0.1f)
.scaleYBy(-0.1f)
.duration = 200
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。