我正在构建一个测验应用。在初始测验屏幕上,我想要一个指令屏幕,告诉用户点击任何地方继续下一个问题。为了达到这个目的,我在FrameLayout
内填了两个LinearLayout
。第一个LinearLayout
包含测验屏幕。第二个LinearLayout
包含带有按钮的指令屏幕(将此按钮称为Ok Button
)。我想在用户点击此Ok Button
时关闭指令屏幕。问题是我无法点击Ok Button
。当用户点击它时,它没有显示任何物理反应(如按钮按下)等,它都不会触发其onClick
方法。
我还为第二个android:clickable="true"
设置了LinearLayout
,以便它响应用户点击而不是将其传递到第一个LinearLayout
我无法理解我的代码有什么问题。任何人都可以帮我解决这个问题。我坚持这个真正的坏事。
got_it_button
):<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:background="#4285f4"
android:id="@+id/mainLayout"
tools:context=".ui.OptionsQuizActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="170dp"
android:background="#33000000"
android:id="@+id/darkQuestionBack"
android:gravity="center">
<TextView
android:id="@+id/qusTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/test_question"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:textColor="#aaffffff"
android:fontFamily="sans-serif-light"
android:textSize="30sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center"
android:layout_height="0dp"
android:paddingLeft="40dp"
android:paddingRight="40dp">
<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="Option One"
android:textColor="#ffffff"
android:id="@+id/optionButton0"
android:background="@drawable/semi_white_button"
android:textSize="24sp"
android:textAllCaps="false"
android:layout_marginBottom="15dp"
android:fontFamily="sans-serif-light" />
<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="Option Two"
android:textColor="#ffffff"
android:id="@+id/optionButton1"
android:background="@drawable/semi_white_button"
android:textSize="24sp"
android:layout_marginBottom="15dp"
android:textAllCaps="false"
android:fontFamily="sans-serif-light" />
<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="Option Three"
android:textColor="#ffffff"
android:id="@+id/optionButton2"
android:background="@drawable/semi_white_button"
android:textSize="24sp"
android:layout_marginBottom="15dp"
android:textAllCaps="false"
android:fontFamily="sans-serif-light"/>
<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="Option Four"
android:textColor="#ffffff"
android:id="@+id/optionButton3"
android:background="@drawable/semi_white_button"
android:textSize="24sp"
android:layout_marginBottom="15dp"
android:textAllCaps="false"
android:fontFamily="sans-serif-light"/>
</LinearLayout>
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
ads:adUnitId="@string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="20dp"
android:clickable="true"
android:paddingBottom="20dp"
android:id="@+id/ll_instructions"
android:background="#dd000000">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/instruction_text"
android:fontFamily="sans-serif-light"
android:textColor="#ddffffff"
android:textSize="30sp"/>
<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="Okay"
android:textColor="#ffffff"
android:id="@+id/got_it_button"
android:background="@drawable/semi_white_button"
android:textSize="24sp"
android:layout_marginTop="25dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:textAllCaps="false"
android:fontFamily="sans-serif-light"/>
</LinearLayout>
</FrameLayout>
和Java代码:
package sprintingminds.com.countrycapitalquiz.ui;
import android.animation.Animator;
import android.animation.ArgbEvaluator;
import android.animation.ValueAnimator;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewAnimator;
import model.ColorGenerator;
import model.QuizGenerator;
import sprintingminds.com.countrycapitalquiz.R;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import java.util.Timer;
import java.util.TimerTask;
public class OptionsQuizActivity extends AppCompatActivity {
private LinearLayout mDarkQuestionBack, mInstructionsLayout;
private FrameLayout mMainLayout;
private Button mOptionButton0, mOptionButton1, mOptionButton2, mOptionButton3, mGotItButton;
private String mOption1, mOption2, mOption3, mOption4;
private TextView mQusTextView;
private QuizGenerator mQuizGenerator;
private ColorGenerator mColorGenerator;
private boolean mNextQuestionCounter, mFirstRunCounter;
private MediaPlayer mMediaCorrect, mMediaWrong;
private ValueAnimator mBackDarkColorAnimator, mBackLightColorAnimator, mBarColorAnimator ,mQuestionFadeInAnimator, mQuestionFadeOutAnimator;
private ValueAnimator mButtonFadeOut,mButton1FadeIn, mButton2FadeIn, mButton3FadeIn, mButton4FadeIn;
private String mNextQuestion;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_options_quiz);
mNextQuestionCounter = false;
mFirstRunCounter = true;
mMediaCorrect = MediaPlayer.create(OptionsQuizActivity.this, R.raw.correct);
mMediaWrong = MediaPlayer.create(OptionsQuizActivity.this, R.raw.wrong);
// Initialize Views
mMainLayout = (FrameLayout) findViewById(R.id.mainLayout);
mDarkQuestionBack = (LinearLayout) findViewById(R.id.darkQuestionBack);
mInstructionsLayout = (LinearLayout) findViewById(R.id.ll_instructions);
mOptionButton0 = (Button) findViewById(R.id.optionButton0);
mOptionButton1 = (Button) findViewById(R.id.optionButton1);
mOptionButton2 = (Button) findViewById(R.id.optionButton2);
mOptionButton3 = (Button) findViewById(R.id.optionButton3);
mQusTextView = (TextView) findViewById(R.id.qusTextView);
mGotItButton = (Button) findViewById(R.id.got_it_button);
// Generating New Question and displaying on screen with Random Color
mQuizGenerator = new QuizGenerator();
mColorGenerator = new ColorGenerator();
displayNextQuestion();
// Initialize Ads
new Timer().schedule(new TimerTask()
{
@Override
public void run()
{
runOnUiThread(new Runnable()
{
@Override
public void run()
{
MobileAds.initialize(getApplicationContext(), "ca-app-pub-96012881833111111/3495512222");
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
});
}
}, 1000);
//Setting On Click Listeners
mOptionButton0.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
checkAnswer(0);
}
});
mOptionButton1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
checkAnswer(1);
}
});
mOptionButton2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
checkAnswer(2);
}
});
mOptionButton3.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
checkAnswer(3);
}
});
mGotItButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
Toast.makeText(OptionsQuizActivity.this,"It's Working!",Toast.LENGTH_SHORT);
}
});
mMainLayout.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
if (mNextQuestionCounter){
mMediaWrong.stop();
mMediaCorrect.stop();
mMediaWrong.release();
mMediaCorrect.release();
mMediaWrong = MediaPlayer.create(OptionsQuizActivity.this,R.raw.wrong);
mMediaCorrect = MediaPlayer.create(OptionsQuizActivity.this,R.raw.correct);
try {
mMediaCorrect.prepare();
mMediaWrong.prepare();
}catch (Exception e){
//do something when unable to play sound
}
displayNextQuestion();
toggleButtons(true);
toggleButtonState();
mNextQuestionCounter=false;
}
}
});
}
public void checkAnswer(int index){
setButtonsAsUnselected();
if(index!=mQuizGenerator.getCorrectAnswerIndex()){
markWrongAnswer(index);
mMediaWrong.start();
}else{
mMediaCorrect.start();
}
markCorrectAnswer(mQuizGenerator.getCorrectAnswerIndex());
toggleButtons(false);
mNextQuestionCounter = true;
}
public void markCorrectAnswer(int index){
switch (index){
case 0:
mOptionButton0.setBackgroundResource(R.drawable.correct_answer_button);
mOptionButton0.setTextColor(Color.parseColor("#ffffffff"));
break;
case 1:
mOptionButton1.setBackgroundResource(R.drawable.correct_answer_button);
mOptionButton1.setTextColor(Color.parseColor("#ffffffff"));
break;
case 2:
mOptionButton2.setBackgroundResource(R.drawable.correct_answer_button);
mOptionButton2.setTextColor(Color.parseColor("#ffffffff"));
break;
case 3:
mOptionButton3.setBackgroundResource(R.drawable.correct_answer_button);
mOptionButton3.setTextColor(Color.parseColor("#ffffffff"));
break;
}
}
public void markWrongAnswer(int index){
switch (index){
case 0:
mOptionButton0.setBackgroundResource(R.drawable.wrong_answer_button);
mOptionButton0.setTextColor(Color.parseColor("#ffffffff"));
break;
case 1:
mOptionButton1.setBackgroundResource(R.drawable.wrong_answer_button);
mOptionButton1.setTextColor(Color.parseColor("#ffffffff"));
break;
case 2:
mOptionButton2.setBackgroundResource(R.drawable.wrong_answer_button);
mOptionButton2.setTextColor(Color.parseColor("#ffffffff"));
break;
case 3:
mOptionButton3.setBackgroundResource(R.drawable.wrong_answer_button);
mOptionButton3.setTextColor(Color.parseColor("#ffffffff"));
break;
}
}
public void toggleButtonState(){
mOptionButton0.setBackgroundResource(R.drawable.semi_white_button);
mOptionButton1.setBackgroundResource(R.drawable.semi_white_button);
mOptionButton2.setBackgroundResource(R.drawable.semi_white_button);
mOptionButton3.setBackgroundResource(R.drawable.semi_white_button);
mOptionButton0.setTextColor(Color.parseColor("#ffffffff"));
mOptionButton1.setTextColor(Color.parseColor("#ffffffff"));
mOptionButton2.setTextColor(Color.parseColor("#ffffffff"));
mOptionButton3.setTextColor(Color.parseColor("#ffffffff"));
}
public void toggleButtons(Boolean status){
mOptionButton0.setClickable(status);
mOptionButton1.setClickable(status);
mOptionButton2.setClickable(status);
mOptionButton3.setClickable(status);
}
public void displayNextQuestion(){
int oldLightColor, oldDarkColor, oldBarColor;
oldLightColor = mColorGenerator.getIntLightColor();
oldDarkColor = mColorGenerator.getIntDarkColor();
oldBarColor = mColorGenerator.getIntBarColor();
mQuizGenerator.nextQuestion();
mColorGenerator.generateNext();
//Setting our next question and options
mNextQuestion = mQuizGenerator.getQuestion();
mOption1 = mQuizGenerator.getOptions(0);
mOption2 = mQuizGenerator.getOptions(1);
mOption3 = mQuizGenerator.getOptions(2);
mOption4 = mQuizGenerator.getOptions(3);
populateNewOptions();
//Checking for first question
if (mFirstRunCounter) {
mMainLayout.setBackgroundColor(mColorGenerator.getIntLightColor());
mDarkQuestionBack.setBackgroundColor(mColorGenerator.getIntDarkColor());
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setNavigationBarColor(mColorGenerator.getIntBarColor());
getWindow().setStatusBarColor(mColorGenerator.getIntBarColor());
}
mQusTextView.setText(mNextQuestion);
mFirstRunCounter = false;
} else {
fadeInOutNextQuestion();
animateBackgroundColors(oldDarkColor, oldLightColor, oldBarColor, mColorGenerator.getIntDarkColor(), mColorGenerator.getIntLightColor(), mColorGenerator.getIntBarColor());
}
}
/* public void fadeInOutOptions(){
mButtonFadeOut = ValueAnimator.ofFloat(1f,0f);
mButton1FadeIn = ValueAnimator.ofFloat(0f,1f);
mButton2FadeIn = ValueAnimator.ofFloat(0f,1f);
mButton3FadeIn = ValueAnimator.ofFloat(0f,1f);
mButton4FadeIn = ValueAnimator.ofFloat(0f,1f);
mButtonFadeOut.setDuration(250);
mButton1FadeIn.setDuration(200);
mButton2FadeIn.setDuration(200);
mButton3FadeIn.setDuration(200);
mButton4FadeIn.setDuration(200);
mButtonFadeOut.setInterpolator(new AccelerateInterpolator());
mButton1FadeIn.setInterpolator(new DecelerateInterpolator());
mButton2FadeIn.setInterpolator(new DecelerateInterpolator());
mButton3FadeIn.setInterpolator(new DecelerateInterpolator());
mButton4FadeIn.setInterpolator(new DecelerateInterpolator());
mButtonFadeOut.start();
mButtonFadeOut.addUpdateListener(new ValueAnimator.AnimatorUpdateListener(){
@Override
public void onAnimationUpdate(ValueAnimator animator){
mOptionButton0.setAlpha((float) animator.getAnimatedValue());
mOptionButton1.setAlpha((float) animator.getAnimatedValue());
mOptionButton2.setAlpha((float) animator.getAnimatedValue());
mOptionButton3.setAlpha((float) animator.getAnimatedValue());
}
});
mButton1FadeIn.addUpdateListener(new ValueAnimator.AnimatorUpdateListener(){
@Override
public void onAnimationUpdate(ValueAnimator animator){
mOptionButton0.setAlpha((float) animator.getAnimatedValue());
mOptionButton1.setAlpha((float) animator.getAnimatedValue());
mOptionButton2.setAlpha((float) animator.getAnimatedValue());
mOptionButton3.setAlpha((float) animator.getAnimatedValue());
}
});
/*
mButton1FadeIn.addListener(new Animator.AnimatorListener(){
@Override
public void onAnimationEnd(Animator animator){
mButton2FadeIn.start();
}
@Override
public void onAnimationCancel(Animator animator){
}
@Override
public void onAnimationRepeat(Animator animator){
}
@Override
public void onAnimationStart(Animator animator){
}
});
mButton2FadeIn.addUpdateListener(new ValueAnimator.AnimatorUpdateListener(){
@Override
public void onAnimationUpdate(ValueAnimator animator){
mOptionButton1.setAlpha((float) animator.getAnimatedValue());
}
});
mButton2FadeIn.addListener(new Animator.AnimatorListener(){
@Override
public void onAnimationEnd(Animator animator){
mButton3FadeIn.start();
}
@Override
public void onAnimationCancel(Animator animator){
}
@Override
public void onAnimationRepeat(Animator animator){
}
@Override
public void onAnimationStart(Animator animator){
}
});
mButton3FadeIn.addUpdateListener(new ValueAnimator.AnimatorUpdateListener(){
@Override
public void onAnimationUpdate(ValueAnimator animator){
mOptionButton2.setAlpha((float) animator.getAnimatedValue());
}
});
mButton3FadeIn.addListener(new Animator.AnimatorListener(){
@Override
public void onAnimationEnd(Animator animator){
mButton4FadeIn.start();
}
@Override
public void onAnimationCancel(Animator animator){
}
@Override
public void onAnimationRepeat(Animator animator){
}
@Override
public void onAnimationStart(Animator animator){
}
});
mButton4FadeIn.addUpdateListener(new ValueAnimator.AnimatorUpdateListener(){
@Override
public void onAnimationUpdate(ValueAnimator animator){
mOptionButton3.setAlpha((float) animator.getAnimatedValue());
}
});
}*/
public void populateNewOptions(){
mOptionButton0.setText(mOption1);
mOptionButton1.setText(mOption2);
mOptionButton2.setText(mOption3);
mOptionButton3.setText(mOption4);
}
public void fadeInOutNextQuestion(){
mQuestionFadeOutAnimator = ValueAnimator.ofFloat(1f,0f);
mQuestionFadeInAnimator = ValueAnimator.ofFloat(0f,1f);
mQuestionFadeOutAnimator.setDuration(200);
mQuestionFadeInAnimator.setDuration(200);
mQuestionFadeOutAnimator.setInterpolator(new AccelerateInterpolator());
mQuestionFadeInAnimator.setInterpolator(new DecelerateInterpolator());
mQuestionFadeOutAnimator.start();
mQuestionFadeOutAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener(){
@Override
public void onAnimationUpdate(ValueAnimator animator){
mQusTextView.setAlpha((float) animator.getAnimatedValue());
}
});
mQuestionFadeInAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener(){
@Override
public void onAnimationUpdate(ValueAnimator animator){
mQusTextView.setAlpha((float) animator.getAnimatedValue());
}
});
mQuestionFadeOutAnimator.addListener(new Animator.AnimatorListener(){
@Override
public void onAnimationEnd(Animator animator){
mQusTextView.setText(mNextQuestion);
mQuestionFadeInAnimator.start();
}
@Override
public void onAnimationCancel(Animator animator){
}
@Override
public void onAnimationRepeat(Animator animator){
}
@Override
public void onAnimationStart(Animator animator){
}
});
}
public void animateBackgroundColors(int oldDarkColor, int oldLightColor, int oldBarColor, int newDarkColor, int newLightColor, int newBarColor){
// Animating dark background color
mBackDarkColorAnimator = ValueAnimator.ofObject(new ArgbEvaluator() , oldDarkColor, newDarkColor);
mBackLightColorAnimator = ValueAnimator.ofObject(new ArgbEvaluator(), oldLightColor, newLightColor);
mBarColorAnimator = ValueAnimator.ofObject(new ArgbEvaluator(), oldBarColor, newBarColor);
mBackDarkColorAnimator.setDuration(200);
mBackLightColorAnimator.setDuration(200);
mBarColorAnimator.setDuration(200);
mBackDarkColorAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animator) {
mDarkQuestionBack.setBackgroundColor((int) animator.getAnimatedValue());
}
});
mBackLightColorAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animator) {
mMainLayout.setBackgroundColor((int) animator.getAnimatedValue());
}
});
mBarColorAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animator) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setNavigationBarColor((int) animator.getAnimatedValue());
getWindow().setStatusBarColor((int) animator.getAnimatedValue());
}
}
});
mBackDarkColorAnimator.start();
mBackLightColorAnimator.start();
mBarColorAnimator.start();
}
public void setButtonsAsUnselected(){
mOptionButton0.setBackgroundResource(R.drawable.unselected_answer_button);
mOptionButton1.setBackgroundResource(R.drawable.unselected_answer_button);
mOptionButton2.setBackgroundResource(R.drawable.unselected_answer_button);
mOptionButton3.setBackgroundResource(R.drawable.unselected_answer_button);
mOptionButton0.setTextColor(Color.parseColor("#44ffffff"));
mOptionButton1.setTextColor(Color.parseColor("#44ffffff"));
mOptionButton2.setTextColor(Color.parseColor("#44ffffff"));
mOptionButton3.setTextColor(Color.parseColor("#44ffffff"));
}
}