android中的多个按钮处理

如何解决android中的多个按钮处理

我有六个按钮

enter image description here

单击其中一个按钮时,请制作动画。因此,我将为其赋予动画效果(单击时会很大)。

因此,如果我给所有按钮一个动画,则单击一个按钮时必须将另一个按钮放回原处。大概只需激活一个按钮 一度 。 代码

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都执行以上代码,那么所有这些动画都会生效,这是不正确的。

我想当一个btn点击另一个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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?