如何实现DialogBox的和onLongClick在同一时间上的按钮? [英] How to Implement DialogBox and onLongClick at the same time on the button?

查看:155
本文介绍了如何实现DialogBox的和onLongClick在同一时间上的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作 onLongClickListner DialogBox的我想补充按钮,当我点击它打开 DialogBox的(它首先问你要选择的按钮)点击DialogBu​​tton 真后它回来选定按钮然后将其持有按钮一段时间检查答案,如果答案是正确的,然后它显示消息的回答是真与错了,它显示消息答案是假。

I am working onLongClickListner and DialogBox I wanted to add button, when i click on it open a dialogBox (It first ask do you want to select the button) after click on the DialogButton True it comes back to selected button then it hold button for a while to check the answer and If the answer is correct then it show Message "Answer is true" and wrong it show Message"Answer is False".

这里是我的code: -

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 view) {

    Button b=(Button) view;
    String answer = b.getText().toString();

    b.setBackgroundResource(R.drawable.ans);
         // disable the button for further clicks.
         b.setEnabled(false);
        //Log.d("Questions", "Valid Checkbox selection made - check if correct");
        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.decrementScore();
        }
        return true;
    }

任何答案是AP preciable。

Any answer is appreciable.

在此先感谢

推荐答案

如果你想显示的按钮,longClick答案应该高度onLongClickListener这个按钮。

If you want to show answer with longClick on button you should attach onLongClickListener to this button.


  1. 实施onLongClickListener到类

  2. 在overrided法onLongClick写code,它描述要显示什么回答。

这篇关于如何实现DialogBox的和onLongClick在同一时间上的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆