Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android中的多按钮处理_Android_Android Studio_Kotlin_Button - Fatal编程技术网

android中的多按钮处理

android中的多按钮处理,android,android-studio,kotlin,button,Android,Android Studio,Kotlin,Button,我有六个钮扣 单击其中一个按钮时,拍摄动画。因此,我会给它一个动画属性(点击时会有很大的规模)来实现这一点 所以,如果我给所有的按钮一个动画,当一个按钮点击它时,另一个按钮必须回到那个位置。实际上,只有一个按钮可以在中激活 一次。 代码: btn1=findviewby(R.id.btn1) btn2=findviewby(R.id.btn2) btn3=findviewby(R.id.btn3) btn4=findviewby(R.id.btn4) btn5=findviewby(R.id.

我有六个钮扣

单击其中一个按钮时,拍摄动画。因此,我会给它一个动画属性(点击时会有很大的规模)来实现这一点

所以,如果我给所有的按钮一个动画,当一个按钮点击它时,另一个按钮必须回到那个位置。实际上,只有一个按钮可以在中激活 一次。 代码:

btn1=findviewby(R.id.btn1)
btn2=findviewby(R.id.btn2)
btn3=findviewby(R.id.btn3)
btn4=findviewby(R.id.btn4)
btn5=findviewby(R.id.btn5)
btn6=findviewby(R.id.btn6)
btn1.setOnClickListener{
btn1.animate()
.scaleXBy(0.1f)
.scaleYBy(0.1f)
.持续时间=200
//如果另一个btn(btn2,3,4,5,6)已经获得动画,则返回该位置
}
如果我为每个btn执行上述代码,则所有btn都会执行动画,并将处于活动状态,这是不对的

我想当一个btn点击另一个btn回到那里的位置


高级

解决方案中的thank's(感谢):在一组按钮中,一次只有一个按钮将转换为给定的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
        }
    }
}
}
class按钮活动:AppCompatActivity(){
private val buttonList=mutableListOf()
var activatedButtonIndex=-1
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_按钮)
val button1=findViewById(R.id.button)
val button2=findViewById(R.id.button2)
val button3=findViewById(R.id.button3)
val按钮4=findViewById(R.id.button4)
val按钮5=findViewById(R.id.button5)
button1.setOnClickListener(监听器)
button2.setOnClickListener(listener)
按钮3.setOnClickListener(listener)
按钮4.setOnClickListener(listener)
按钮5.setOnClickListener(listener)
//设置标记以动态标识foreach中的对象,而不是资源id
button1.tag=0
按钮2.tag=1
按钮3.tag=2
按钮4.tag=3
按钮5.tag=4
按钮列表。添加(按钮1)
按钮列表。添加(按钮2)
按钮列表。添加(按钮3)
按钮列表。添加(按钮4)
按钮列表。添加(按钮5)
}
private val listener=View.OnClickListener{View->
val标记:Int=view.tag为Int
风景?让我看看{
if(activatedButtonIndex==标记){
return@let
}
startTranslateAnim(it)
if(activatedButtonIndex!=标记){
重置转换NIM(激活的按钮索引)
}
activatedButtonIndex=标记
}
}
私人娱乐startTranslateAnim(it:View){
it.animate()
.scaleXBy(0.1f)
.scaleYBy(0.1f)
.持续时间=200
}
private fun ResetTranslationAnm(以前选择的索引:Int){
按钮列表?让我看看{
it.foreachinedexed{索引,按钮->
如果(索引==以前选择的索引)
button.animate().scaleXBy(-0.1f)
.scaleYBy(-0.1f)
.持续时间=200
}
}
}
}

您能详细描述一下您的问题吗?不清楚,我将编辑我的问题。如果我理解正确,您有6个按钮,当您单击1个按钮时,它将在上面翻译动画。假设如果单击另一个按钮,它必须使用动画进行转换,并且需要重置上一个按钮的动画转换,以便一次一个按钮应该更大。对所有按钮重复相同的操作。Rite?是的,兄弟。没错,它起作用了。谢谢你的身体。。。但对我来说,作为一个新的开发人员,很难理解到底发生了什么。。。
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
        }
    }
}
}