如何在按钮上同时实现DialogBox和onLongClick?

Posted

技术标签:

【中文标题】如何在按钮上同时实现DialogBox和onLongClick?【英文标题】:How to Implement DialogBox and onLongClick at the same time on the button? 【发布时间】:2013-10-01 04:03:15 【问题描述】:

我正在工作onLongClickListnerDialogBox 我想添加button,当我点击它时打开一个dialogBox(它首先询问你是否要选择按钮),然后点击对话框按钮@987654325 @ 它回到选定的button 然后它持有button 一段时间以检查答案,如果答案正确,则显示消息“答案为真”,错误则显示消息“答案为假”。

这是我的代码:-

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;
    

任何答案都是可观的。

提前致谢

【问题讨论】:

【参考方案1】:

如果您想通过 longClick on 按钮显示答案,您应该将 onLongClickListener 附加到此按钮。

    在你的类中实现 onLongClickListener 在重写的方法“onLongClick”中编写代码来描述您想要显示的答案。

【讨论】:

欢迎回复!!我想添加 Dailog First onclick 按钮,然后我想按住我的按钮检查一会儿。如果我的答案是正确的,那么它会显示你的答案是正确的消息或不是。

以上是关于如何在按钮上同时实现DialogBox和onLongClick?的主要内容,如果未能解决你的问题,请参考以下文章

Win32 对话框上的 WM_CLOSE 和 IDCANCEL

JavaFX如何实现模态窗口

SAP ABAP ALV 的一些总结:Docking container 和 Dialogbox container

如何防止在 UINavigationbar 上同时触摸按钮的双重动作?

MFC的Dialogbox多行文本框(CEdit)有最大字符限制,默认最大显示长度是?怎么实现显示160KB大小的文件

通过按钮同时显示图像和文本字符串[复制]