如果选中了buttonTut,如何更改buttonTut?

时间:2017-08-15 19:14:41

标签: java android if-statement colors radio-button

用户可以在4个不同的radioButton之间进行选择,每个人都会设置不同的枚举。

一开始我只是将它们放在我的布局中的radioGroup中,我发现来自radioButton s(黑色)的圆圈和圆点的标准颜色是“隐藏”引起的深蓝色的背景。我试了一下,发现buttonTint是改变radioButton s中圆/点颜色的正确属性。

现在我正在尝试编写一个if语句(在if语句中,请参阅下面的代码),如果选中它,则会导致圆圈/点的颜色从radioButton更改。这应该发生之前我“激活”checking方法/按钮。

我尝试了一下,我所学到的只有5种不同的方式,它们改变颜色。

我将使用XXXX标记无效/缺失的方法。

Java代码:

public void setKnowledge() {
    setContentView(R.layout.knowledge_level);

    Button checking = (Button) findViewById(R.id.buttonChecking);

    checking.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            checkSelection();
            categoryAuswahl();
        }
    });
}

public knowledge checkSelection() {
    RadioButton rbNoob = (RadioButton) findViewById(R.id.radioButtonNoob);
    RadioButton rbBeginner = (RadioButton) findViewById(R.id.radioButtonBeginner);
    RadioButton rbAdvanced = (RadioButton) findViewById(R.id.radioButtonAdv);
    RadioButton rbPro = (RadioButton) findViewById(R.id.radioButtonPro);
    RadioButton rbGrandMaster = (RadioButton) findViewById(R.id.radioButtonGM);

    if(rbNoob.isChecked()) {
        if(rbNoob.XXXX()) {
            rbNoob.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#511613")));
        } else {
            rbNoob.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#E0E0E0")));
        }
        return knowledge.NOOB;
    }
    else if(rbBeginner.isChecked()) {
        return knowledge.BEGINNER;
    }
    else if(rbAdvanced.isChecked()) {
        return knowledge.ADVANCED;
    }
    else if(rbPro.isChecked()) {
        return knowledge.PRO;
    }
    else if(rbGrandMaster.isChecked()) {
        return knowledge.GM;
    }
    return null;
}

作为XXXX,我试过“isSelected();”,“isPressed();”,“isEnabled();”,“isActivated();”和“isChecked();”但是一切都行不通。

当用户按下radioButton时,颜色应该会改变,所以“实时”。

我的XML(如果已知)[knowledge_level.xml]

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:id="@+id/layoutTestwissenstand"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#081929">

<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="32dp"
    app:layout_constraintTop_toBottomOf="@+id/testWissenstandText"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:id="@+id/radioGroup"
    app:layout_constraintRight_toLeftOf="@+id/buttonChecking"
    android:layout_marginRight="8dp"
    app:layout_constraintHorizontal_bias="0.538">

    <RadioButton
        android:id="@+id/radioButtonNoob"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginTop="8dp"
        android:checked="true"
        android:text="Keine Vorkenntnisse"
        android:textAlignment="center"
        android:textColor="#E0E0E0"
        android:textSize="18sp"
        android:textStyle="bold"
        app:buttonTint="#E0E0E0"
        app:layout_constraintHorizontal_bias="0.505"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/buttonChecking"
        app:layout_constraintTop_toBottomOf="@+id/testWissenstandText"/>

    <RadioButton
        android:id="@+id/radioButtonBeginner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginTop="8dp"
        android:checked="true"
        android:text="Anfänger"
        android:textAlignment="center"
        android:textColor="#E0E0E0"
        android:textSize="18sp"
        android:textStyle="bold"
        app:buttonTint="#E0E0E0"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="@+id/radioButtonNoob"
        app:layout_constraintRight_toRightOf="@+id/radioButtonNoob"
        app:layout_constraintTop_toBottomOf="@+id/radioButtonNoob"/>

    <RadioButton
        android:id="@+id/radioButtonAdv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginTop="8dp"
        android:text="Fortgeschrittener"
        android:textAlignment="center"
        android:textColor="#E0E0E0"
        android:textSize="18sp"
        android:textStyle="bold"
        app:buttonTint="#E0E0E0"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="@+id/radioButtonBeginner"
        app:layout_constraintRight_toRightOf="@+id/radioButtonBeginner"
        app:layout_constraintTop_toBottomOf="@+id/radioButtonBeginner"/>

    <RadioButton
        android:id="@+id/radioButtonPro"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginTop="8dp"
        android:text="Pro"
        android:textAlignment="center"
        android:textColor="#E0E0E0"
        android:textSize="18sp"
        android:textStyle="bold"
        app:buttonTint="#E0E0E0"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="@+id/radioButtonAdv"
        app:layout_constraintRight_toRightOf="@+id/radioButtonAdv"
        app:layout_constraintTop_toBottomOf="@+id/radioButtonAdv"/>

    <RadioButton
        android:id="@+id/radioButtonGM"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginTop="8dp"
        android:text='"Grand Master"'
        android:textAlignment="center"
        android:textColor="#E0E0E0"
        android:textSize="18sp"
        android:textStyle="bold"
        app:buttonTint="#E0E0E0"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="@+id/radioButtonPro"
        app:layout_constraintRight_toRightOf="@+id/radioButtonPro"
        app:layout_constraintTop_toBottomOf="@+id/radioButtonPro"/>

</RadioGroup>

<ImageView
    android:id="@+id/kastenKnowledge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="16dp"
    app:layout_constraintHorizontal_bias="0.517"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/kasten"/>

<TextView
    android:id="@+id/testWissenstandHeader"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="Dein Wissensstand?"
    android:textAlignment="center"
    android:textColor="#511613"
    android:textSize="32sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="@+id/kastenKnowledge"
    app:layout_constraintLeft_toLeftOf="@+id/kastenKnowledge"
    app:layout_constraintRight_toRightOf="@+id/kastenKnowledge"
    app:layout_constraintTop_toTopOf="@+id/kastenKnowledge"/>

<TextView
    android:id="@+id/testWissenstandText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="24dp"
    android:text='Wie würdest du deinen Wissenstand\nbezüglich dem Spieleinhalt\nvon "Overwatch" beurteilen?'
    android:textAlignment="center"
    android:textColor="#E0E0E0"
    android:textSize="20sp"
    app:layout_constraintHorizontal_bias="0.512"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/testWissenstandHeader"/>

<Button
    android:id="@+id/buttonChecking"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="0dp"
    android:background="@drawable/button_box"
    android:text="C\nL\nI\nC\nK"
    android:textAllCaps="false"
    android:textColor="#511613"
    android:textSize="16sp"
    android:textStyle="bold"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginRight="16dp"
    app:layout_constraintBottom_toBottomOf="@+id/radioGroup"
    android:layout_marginBottom="8dp"
    app:layout_constraintTop_toTopOf="@+id/radioGroup"
    android:layout_marginTop="8dp"
    app:layout_constraintVertical_bias="0.5"/>



<TextView
    android:id="@+id/knowledgeHint"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="0dp"
    android:layout_marginRight="0dp"
    android:text="Wenn du einen Wissenstand\nfestgelegt hast, drücke den\nKnopf daneben!"
    android:textAlignment="center"
    android:textColor="#FF4400"
    android:textSize="20sp"
    android:textStyle="bold|italic"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginTop="32dp"
    app:layout_constraintTop_toBottomOf="@+id/radioGroup"/>

</android.support.constraint.ConstraintLayout>

代码应该用java编写,如果可能的话应该避免使用jQuery / javascript(或c#)。

2 个答案:

答案 0 :(得分:0)

您正尝试更改单选按钮tintcolor setOnCheckedChange /按钮单击右键?

注意:如果您需要动态更改色调颜色,则您的API级别必须为21及以上,然后才会应用

您在按钮点击时再次重新初始化单选按钮,因此所有单选按钮都会再次创建。

你必须喜欢这个

final RadioButton rbNoob = (RadioButton) findViewById(R.id.rbNoob);

1)。如果你想在setOnCheckedChange中使用chnage tintcolor

 rbNoob.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b)
                {
                    rbNoob.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#511613")));
                }
            }
        });

2)。如果你想在按钮上点击

更改tintcolor
button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(rbNoob.isChecked())
                    rbNoob.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#511613")));
                else
                    rbNoob.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#E0E0E0")));
            }
        });

希望这会对你有帮助......

答案 1 :(得分:0)

Button checking;
RadioButton rbNoob,rbBeginner,rbAdvanced,rbPro,rbGrandMaster;

 public void setKnowledge() {
        setContentView(R.layout.knowledge_level);

        checking = (Button) findViewById(R.id.buttonChecking);
            rbNoob = (RadioButton) findViewById(R.id.radioButtonNoob);
        rbBeginner = (RadioButton) findViewById(R.id.radioButtonBeginner);
        rbAdvanced = (RadioButton) findViewById(R.id.radioButtonAdv);
        rbPro = (RadioButton) findViewById(R.id.radioButtonPro);
        rbGrandMaster = (RadioButton) findViewById(R.id.radioButtonGM);
        checking.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                checkSelection();
                categoryAuswahl();
            }
        });
    }

    public knowledge checkSelection() {


        if(rbNoob.isChecked()) {
            if(rbNoob.XXXX()) {
                rbNoob.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#511613")));
            } else {
                rbNoob.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#E0E0E0")));
            }
            return knowledge.NOOB;
        }
        else if(rbBeginner.isChecked()) {
            return knowledge.BEGINNER;
        }
        else if(rbAdvanced.isChecked()) {
            return knowledge.ADVANCED;
        }
        else if(rbPro.isChecked()) {
            return knowledge.PRO;
        }
        else if(rbGrandMaster.isChecked()) {
            return knowledge.GM;
        }
        return null;
    }