android – 如何在点击时更改按钮的颜色并暂停屏幕几秒钟?

android – 如何在点击时更改按钮的颜色并暂停屏幕几秒钟?,第1张

概述我正在研究 android测验.我想添加按钮.当我点击更改图像或颜色并按住按钮一段时间来检查答案,如果答案是正确的,那么它显示绿色和错误的红色. 我已经在最后按钮上实现了颜色更改,但我不明白如何实现Handler延迟: – public class QuestionActivity extends Activity implements OnClickListener{ private 我正在研究 android测验.我想添加按钮.当我点击它更改图像或颜色并按住按钮一段时间来检查答案,如果答案是正确的,那么它显示绿色和错误的红色.

我已经在最后按钮上实现了颜色更改,但我不明白如何实现Handler延迟: –

public class QuestionActivity extends Activity implements OnClickListener{    private Question currentQ;    private GamePlay currentGame;    @OverrIDe    public voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.question);        processSession();    }    private voID processSession(){                /**         * Configure current game and get question         */        currentGame = ((CYKApplication)getApplication()).getCurrentGame();        currentQ = currentGame.getNextQuestion();        button nextBtn1 = (button) findVIEwByID(R.ID.answer1);        nextBtn1.setonClickListener(this);        button nextBtn2 = (button) findVIEwByID(R.ID.answer2);        nextBtn2.setonClickListener(this);        button nextBtn3 = (button) findVIEwByID(R.ID.answer3);        nextBtn3.setonClickListener(this);        button nextBtn4 = (button) findVIEwByID(R.ID.answer4);        nextBtn4.setonClickListener(this);        button nextBtn5 = (button) findVIEwByID(R.ID.answer5);        nextBtn5.setonClickListener(this);        /**         * Update the question and answer options..         */        setQuestions();    }    /**     * Method to set the text for the question and answers from the current games     * current question     */    private voID setQuestions() {        //set the question text from current question        String question = Utility.cAPItalise(currentQ.getQuestion());        TextVIEw qText = (TextVIEw) findVIEwByID(R.ID.question);        qText.setText(question);        //set the available options        List<String> answers = currentQ.getQuestionoptions();        TextVIEw option1 = (TextVIEw) findVIEwByID(R.ID.answer1);        option1.setText(Utility.cAPItalise(answers.get(0)));        TextVIEw option2 = (TextVIEw) findVIEwByID(R.ID.answer2);        option2.setText(Utility.cAPItalise(answers.get(1)));        TextVIEw option3 = (TextVIEw) findVIEwByID(R.ID.answer3);        option3.setText(Utility.cAPItalise(answers.get(2)));        TextVIEw option4 = (TextVIEw) findVIEwByID(R.ID.answer4);        option4.setText(Utility.cAPItalise(answers.get(3)));        int score = currentGame.getscore();        String scr = String.valueOf(score);        TextVIEw score1 = (TextVIEw) findVIEwByID(R.ID.score);        score1.setText(scr);      try{          new CountDownTimer(20000,1000) {            public voID onTick(long millisUntilFinished) {                TextVIEw timers = (TextVIEw) findVIEwByID(R.ID.timers);                timers.setText("" + millisUntilFinished / 1000);            }            public voID onFinish() {                 currentGame.decrementscore();                processSession();                   }         }.start();      }      catch(Exception ex)      {          throw new RuntimeException(ex);      }        }    @OverrIDe    public voID onClick(VIEw arg0) {        //Log.d("Questions","Moving to next question");        if(arg0.getID()==R.ID.answer5)        {        new AlertDialog.Builder(this).setMessage("Are you sure?").setCancelable(true).setPositivebutton("Yes",new DialogInterface.OnClickListener() {        public voID onClick(DialogInterface dialog,int ID) {                finish();                 }             }).setNegativebutton("No",null).show();                }        else        {            if(!checkAnswer(arg0)) return;          /**         * check if end of game         */        if (currentGame.isGameOver()){            //Log.d("Questions","End of game! lets add up the scores..");            //Log.d("Questions","Questions Correct: " + currentGame.getRight());            //Log.d("Questions","Questions Wrong: " + currentGame.getWrong());            Intent i = new Intent(this,EndgameActivity.class);            startActivity(i);            finish();        }        else{            Intent i = new Intent(this,QuestionActivity.class);            startActivity(i);            finish();          }        }      }    @OverrIDe    public boolean onKeyDown(int keyCode,KeyEvent event)    {        switch (keyCode)        {        case KeyEvent.KEYCODE_BACK :            return true;        }        return super.onKeyDown(keyCode,event);    }    /**     * Check if a checkBox has been selected,and if it     * has then check if its correct and update gamescore     */    private boolean checkAnswer(VIEw v) {     try {     final button b=(button) v;     final String answer = b.getText().toString();     counterTimer.cancel();     b.setBackgroundResource(R.drawable.ans);     b.setEnabled(false);     //Log.d("Questions","ValID CheckBox selection made - check if correct");         handler.postDelayed(new Runnable() {            @OverrIDe            public voID run() {                 if( (currentQ.getAnswer().equalsIgnoreCase(answer)))                {                    b.setBackgroundResource(R.drawable.ansgreen);                    //Log.d("Questions","Correct Answer!");                    currentGame.incrementscore();                }                else{                    b.setBackgroundResource(R.drawable.ansred);                    //Log.d("Questions","Incorrect Answer!");                    currentGame.decrementscore1();                }           }         },10000000);       } catch (Exception e) {           e.printstacktrace();       }     return true;}

任何答案都很明显.

提前致谢

解决方法 我已经在按钮上实现了这个代码来改变按钮上的颜色.希望它适合你.

private boolean checkAnswer(VIEw v) {            button b=(button) v;            String answer = b.getText().toString();            b.setBackgroundResource(R.drawable.ans);                 b.setEnabled(false);                if (currentQ.getAnswer().equalsIgnoreCase(answer))                {                    b.setBackgroundResource(R.drawable.ansgreen);                }                else{                    b.setBackgroundResource(R.drawable.ansred);                }                return true;            }

处理器

if (currentGame.isGameOver()){            //Log.d("Questions","Questions Wrong: " + currentGame.getWrong());            final Handler handle = new Handler();            Runnable delay = new Runnable() {                public voID run() {                    finish();                    startActivity(new Intent(QuestionActivity.this,EndgameActivity.class));                }            };            handle.postDelayed(delay,1000);        }        else        {            final Handler handle = new Handler();            Runnable delay = new Runnable() {                public voID run() {                    finish();                    startActivity(new Intent(QuestionActivity.this,QuestionActivity.class));                }            };            handle.postDelayed(delay,1000);                  }              }
总结

以上是内存溢出为你收集整理的android – 如何在点击时更改按钮的颜色并暂停屏幕几秒钟?全部内容,希望文章能够帮你解决android – 如何在点击时更改按钮的颜色并暂停屏幕几秒钟?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1129942.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存