我尝试将按钮设为绿色,如果回答是corect,将按钮设为红色,如果答案错误,则将其重置为默认状态。我试图将R.drawable代码放在不同的位置,但都不起作用,并且在我认为我必须添加一个睡眠计时器才能看到效果?我确信我只是将代码放在错误的位置,因为我认为使用此代码应该可以工作。请有人启发我:)
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.Random;
public class Game extends AppCompatActivity {
AdView adView;
Button ans1_btn, ans2_btn, ans3_btn, ans4_btn, back_btn;
TextView question, score;
private FirebaseDatabase mFirebaseDatabase;
private FirebaseAuth mAuth;
private DatabaseReference myRef;
String userId;
private Questions mQuestions = new Questions();
Random r;
private String mAnswer;
private int mQuestionsLength = mQuestions.mQuestions.length;
int uScore;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
//Initiate
r = new Random();
adView = findViewById(R.id.add);
ans1_btn = findViewById(R.id.ans1_btn);
ans2_btn = findViewById(R.id.ans2_btn);
ans3_btn = findViewById(R.id.ans3_btn);
ans4_btn = findViewById(R.id.ans4_btn);
back_btn = findViewById(R.id.back_btn);
question = findViewById(R.id.question);
score = findViewById(R.id.score);
mAuth = FirebaseAuth.getInstance();
mFirebaseDatabase = FirebaseDatabase.getInstance();
myRef = mFirebaseDatabase.getReference();
final FirebaseUser user = mAuth.getCurrentUser();
assert user != null;
userId = user.getUid();
myRef.child("users").child(userId).child("eScore").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
uScore = dataSnapshot.getValue(Integer.class);
score.setText(String.valueOf(uScore));
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
//...//
// update question randomizer
updateQuestion(r.nextInt(mQuestionsLength));
//...//
//Button Functions
ans1_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ans1_btn.getText() == mAnswer){
uScore+=10;
Toast.makeText(Game.this,"Corect", Toast.LENGTH_SHORT).show();
score.setText("Score: "+ String.valueOf(uScore));
myRef.child("users").child(userId).child("eScore").setValue(uScore);
ans1_btn.setBackgroundResource(R.drawable.corect_btn);
resetButtons();
updateQuestion(r.nextInt(mQuestionsLength));
}else
gameOver();
}
});
ans2_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ans2_btn.getText() == mAnswer){
Toast.makeText(Game.this,"Corect", Toast.LENGTH_SHORT).show();
uScore+=10;
score.setText("Score: "+ String.valueOf(uScore));
myRef.child("users").child(userId).child("eScore").setValue(uScore);
ans2_btn.setBackgroundResource(R.drawable.corect_btn);
resetButtons();
updateQuestion(r.nextInt(mQuestionsLength));
}else
gameOver();
}
});
ans3_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ans3_btn.getText() == mAnswer){
Toast.makeText(Game.this,"Corect", Toast.LENGTH_SHORT).show();
uScore+=10;
score.setText("Score: "+ String.valueOf(uScore));
myRef.child("users").child(userId).child("eScore").setValue(uScore);
ans3_btn.setBackgroundResource(R.drawable.corect_btn);
resetButtons();
updateQuestion(r.nextInt(mQuestionsLength));
}else
gameOver();
}
});
ans4_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ans4_btn.getText() == mAnswer){
Toast.makeText(Game.this,"Corect", Toast.LENGTH_SHORT).show();
uScore+=10;
score.setText("Score: "+ String.valueOf(uScore));
myRef.child("users").child(userId).child("eScore").setValue(uScore);
ans1_btn.setBackgroundResource(R.drawable.corect_btn);
resetButtons();
updateQuestion(r.nextInt(mQuestionsLength));
}else
gameOver();
}
});
back_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Game.this,Home.class);
startActivity(i);
}
});
//...//
// Full Screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
//.....//
// AdMob add - Banner
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
adView.loadAd(adRequest);
//...//
}
// Update Question Function
private void updateQuestion(int num){
question.setText(mQuestions.getQuestions(num));
ans1_btn.setText(mQuestions.getChoice1(num));
ans2_btn.setText(mQuestions.getChoice2(num));
ans3_btn.setText(mQuestions.getChoice3(num));
ans4_btn.setText(mQuestions.getChoice4(num));
mAnswer = mQuestions.getCorrectAnswer(num);
resetButtons();
}
public void resetButtons(){
ans1_btn.setBackgroundResource(R.drawable.default_btn);
ans2_btn.setBackgroundResource(R.drawable.default_btn);
ans3_btn.setBackgroundResource(R.drawable.default_btn);
ans4_btn.setBackgroundResource(R.drawable.default_btn);
}
//...//
//Other Functions
private void gameOver(){
uScore -=5;
Toast.makeText(Game.this,"Wrong", Toast.LENGTH_SHORT).show();
if (uScore < 0){
uScore = 0;
score.setText("0");
}
score.setText("Score: "+ String.valueOf(uScore));
myRef.child("users").child(userId).child("eScore").setValue(uScore);
if (ans1_btn.getText() != mAnswer){
ans1_btn.setBackgroundResource(R.drawable.wrong_btn);
}else if (ans2_btn.getText()!=mAnswer){
ans2_btn.setBackgroundResource(R.drawable.wrong_btn);
}else if (ans3_btn.getText()!=mAnswer) {
ans3_btn.setBackgroundResource(R.drawable.wrong_btn);
}else if (ans4_btn.getText()!=mAnswer) {
ans4_btn.setBackgroundResource(R.drawable.wrong_btn);
}
resetButtons();
updateQuestion(r.nextInt(mQuestionsLength));
}
//...//
}
问题在这里