Android按钮颜色更改为其他颜色



我正在创建一个简单的问答应用程序。如果答案正确,请将按钮的颜色更改为绿色,如果答案不正确,请更改为红色。如果我通过布局的xml改变颜色,它通常会改变,但当我写时

if (option1.getText().equals(correctAnswer)){
option1.setBackgroundColor(R.color.green);
Toast.makeText(this, "Answer is correct", Toast.LENGTH_SHORT).show();
}

颜色变化,但完全变为另一种颜色(深蓝色(。为什么颜色没有按预期改变?

themes.xml:

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.IslomniOrganamiz" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/white</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SavolJavob"
android:gravity="center_vertical"
android:background="@color/black">
<TextView
android:id="@+id/whichQuestion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1 - savol"
android:textColor="@color/white"
android:textSize="40sp"
android:layout_gravity="center_horizontal"
/>
<TextView
android:id="@+id/questionTextView"
android:layout_width="match_parent"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_height="wrap_content"
android:layout_marginTop="180dp"
android:layout_gravity="center_vertical"
android:text="Payg'ambarimiz Muhammad(s.a.v) qaysi shaharda tavallud topganlar?"
android:textColor="@color/white"
android:textSize="24sp" />
<LinearLayout
android:layout_marginTop="200dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
style="@style/Theme.IslomniOrganamiz"
android:tag="0"
android:layout_width="200dp"
android:id="@+id/option1"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="@color/black"
android:text="Makkada"
android:paddingRight="25dp"
android:paddingLeft="25dp"/>
<Button
android:tag="1"
android:id="@+id/option2"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="@color/black"
android:text="Madinada" />
<Button
android:tag="2"
android:id="@+id/option3"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="@color/black"
android:text="Rimda"/>
<Button
android:tag="3"
android:id="@+id/option4"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="@color/black"
android:text="To'gri javob yo'q" />
</LinearLayout>
</LinearLayout>

Java:

package com.example.islomniorganamiz;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Random;
public class SavolJavob extends AppCompatActivity implements View.OnClickListener {
Random random=new Random();
int questionNumber=1;
TextView questionTextView,whichQuestion;
Button option1,option2,option3,option4;
Questions questions=new Questions();
ArrayList<Integer> askedQuestions = new ArrayList<Integer>();
int random_button1;
int random_button2;
int random_button3;
int random_button4;
int random_number;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_savol_javob);
questionTextView=findViewById(R.id.questionTextView);
whichQuestion=findViewById(R.id.whichQuestion);
option1=findViewById(R.id.option1);
option2=findViewById(R.id.option2);
option3=findViewById(R.id.option3);
option4=findViewById(R.id.option4);
option1.setOnClickListener(this);
option2.setOnClickListener(this);
option3.setOnClickListener(this);
option4.setOnClickListener(this);

nextQuestion();
}

@Override
public void onClick(View v) {
String correctAnswer=questions.correctAnswersArray[random_number];
switch (v.getId()){
case R.id.option1:
if (option1.getText().equals(correctAnswer)){
option1.setBackgroundColor(R.color.green);
Toast.makeText(this, "Answer is correct", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(this, "Incorrect Button1.Correct answer was: "+questions.correctAnswersArray[random_number], Toast.LENGTH_SHORT).show();
}
nextQuestion();
break;
case R.id.option2:
if (option2.getText().equals(correctAnswer)){
Toast.makeText(this, "Answer is correct", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "Incorrect Button2.Correct answer was: "+questions.correctAnswersArray[random_number], Toast.LENGTH_SHORT).show();
}
nextQuestion();
break;
case R.id.option3:
if (option3.getText().equals(correctAnswer)){
Toast.makeText(this, "Answer is correct", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "Incorrect Button3.Correct answer was: "+questions.correctAnswersArray[random_number], Toast.LENGTH_SHORT).show();
}
nextQuestion();
break;
case R.id.option4:
if (option4.getText().equals(correctAnswer)){
Toast.makeText(this, "Answer is correct", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "Incorrect Button4.Correct answer was: "+questions.correctAnswersArray[random_number], Toast.LENGTH_SHORT).show();
}
nextQuestion();
break;
}
}

@SuppressLint("ResourceAsColor")
public void nextQuestion(){
random_number=random.nextInt(questions.questionsArray.length);
while ((askedQuestions.contains(random_number))){
random_number=random.nextInt(questions.questionsArray.length);
}
askedQuestions.add(random_number);
random_button1=random.nextInt(4);
while (random_button1==random_button2) {
random_button2 = random.nextInt(4);
}
while (random_button3==random_button1||random_button3==random_button2) {
random_button3 = random.nextInt(4);
}
while (random_button4==random_button1||random_button4==random_button2||random_button4==random_button3) {
random_button4 = random.nextInt(4);
}
whichQuestion.setText(questionNumber+" - SAVOL");
questionTextView.setText(questions.questionsArray[random_number]);
option1.setText(questions.optionsArray[random_number][random_button1]);
option2.setText(questions.optionsArray[random_number][random_button2]);
option3.setText(questions.optionsArray[random_number][random_button3]);
option4.setText(questions.optionsArray[random_number][random_button4]);
questionNumber++;
}

}

之所以会发生这种情况,是因为方法setBackgroundColor(@ColorInt int color)使用的是@ColorInt,而不是资源(@ColorRes(

用途:

button.setBackgroundColor(ContextCompat.getColor(this,R.color.xxxx));

或者类似的东西:

button.setBackgroundColor(Color.GREEN)

相关内容

最新更新