如何在不按下按钮的情况下开始活动?

问题描述:

我有一个询问用户的问题。目前它会一直提问,直到用户退出应用程序,但我希望它提出20个问题,然后继续进行新的活动。我想如果我这样做:如何在不按下按钮的情况下开始活动?

for(int i = 0;i < 20;i++){ 
      random = new Random(); 
      chooseQuestion(); 
} 
Intent i = new Intent(PlayGame.this, Gamble.class); 
startActivity(i); 

它会工作,但我的应用程序现在打破。我是否错误地开了一个新的活动,如果是的话,我应该怎么做?

好吧我真的很陌生,我不知道有多少代码要回复评论,所以这里就是所有这些,我很抱歉。

public class PlayGame extends Activity implements OnClickListener { 
    //set up minimum and maximum numbers for different operators and difficulty levels 
    private int level = 0, answer = 0, operator = 0, operand1 = 0, operand2 = 0; 
    private final int ADD_OPERATOR = 0, SUBTRACT_OPERATOR = 1, MULTIPLY_OPERATOR = 2, DIVIDE_OPERATOR = 3; 
    private String[] operators = {"+", "-", "x", "/"}; 
    private int[][] levelMin = { 
      {1, 11, 21}, 
      {1, 5, 10}, 
      {2, 5, 10}, 
      {2, 3, 5}}; 
    private int[][] levelMax = { 
      {10, 25, 50}, 
      {10, 20, 30}, 
      {5, 10, 15}, 
      {10, 50, 100}}; 
    private Random random; 
    private TextView question, answerTxt, scoreTxt, coincountTxt; 
    private ImageView response; 
    private Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0, enterBtn, clearBtn; 

    @Override 
    //tell the buttons to be buttons when the activity is opened 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_playgame); 
     question = (TextView)findViewById(R.id.question); 
     answerTxt = (TextView)findViewById(R.id.answer); 
     response = (ImageView)findViewById(R.id.response); 
     scoreTxt = (TextView)findViewById(R.id.score); 
     coincountTxt = (TextView)findViewById(R.id.coincount); 
     response.setVisibility(View.INVISIBLE); 
     btn1 = (Button)findViewById(R.id.btn1); 
     btn2 = (Button)findViewById(R.id.btn2); 
     btn3 = (Button)findViewById(R.id.btn3); 
     btn4 = (Button)findViewById(R.id.btn4); 
     btn5 = (Button)findViewById(R.id.btn5); 
     btn6 = (Button)findViewById(R.id.btn6); 
     btn7 = (Button)findViewById(R.id.btn7); 
     btn8 = (Button)findViewById(R.id.btn8); 
     btn9 = (Button)findViewById(R.id.btn9); 
     btn0 = (Button)findViewById(R.id.btn0); 
     enterBtn = (Button)findViewById(R.id.enter); 
     clearBtn = (Button)findViewById(R.id.clear); 
     btn1.setOnClickListener(this); 
     btn2.setOnClickListener(this); 
     btn3.setOnClickListener(this); 
     btn4.setOnClickListener(this); 
     btn5.setOnClickListener(this); 
     btn6.setOnClickListener(this); 
     btn7.setOnClickListener(this); 
     btn8.setOnClickListener(this); 
     btn9.setOnClickListener(this); 
     btn0.setOnClickListener(this); 
     enterBtn.setOnClickListener(this); 
     clearBtn.setOnClickListener(this); 
     Bundle extras = getIntent().getExtras(); 
     if(extras != null) 
     { 
      int passedLevel = extras.getInt("level", -1); 
      if(passedLevel>=0) level = passedLevel; 


     } 
     for(int i = 0;i < 20;i++){ 
      random = new Random(); 
      chooseQuestion(); 
     } 
     Intent i = new Intent(PlayGame.this, Gamble.class); 
     startActivity(i); 
    } 
    //find which button pressed on main menu and set operator accordingly 
    public int getOperator() { 
     Bundle extras = getIntent().getExtras(); 
     int type = extras.getInt("type", -1); 
     if(type==1) operator = ADD_OPERATOR; 
     else if(type==2) operator = SUBTRACT_OPERATOR; 
     else if(type==3) operator = MULTIPLY_OPERATOR; 
     else if(type==4) operator = DIVIDE_OPERATOR; 
     //randomly finds operator for each successive question 
     else if(type==5) operator = random.nextInt(operators.length); 
     return operator; 
    } 
    //get random valid question within the parameters defined by operation and difficulty 
    private void chooseQuestion(){ 
     //get a question 
     answerTxt.setText("= ?"); 
     operator = getOperator(); 
     operand1 = getOperand(); 
     operand2 = getOperand(); 
     //get new subtraction question if answer is negative 
     if(operator == SUBTRACT_OPERATOR){ 
      while(operand2>operand1){ 
       operand1 = getOperand(); 
       operand2 = getOperand(); 
      } 
     } 
     //get new division question if answer is not whole number 
     else if(operator==DIVIDE_OPERATOR){ 
      while((((double)operand1/(double)operand2)%1 > 0) || (operand1==operand2)) 
      { 
       operand1 = getOperand(); 
       operand2 = getOperand(); 
      } 
     } 
     //find answer to question 
     switch(operator) 
     { 
      case ADD_OPERATOR: 
       answer = operand1+operand2; 
       break; 
      case SUBTRACT_OPERATOR: 
       answer = operand1-operand2; 
       break; 
      case MULTIPLY_OPERATOR: 
       answer = operand1*operand2; 
       break; 
      case DIVIDE_OPERATOR: 
       answer = operand1/operand2; 
       break; 
      default: 
       break; 
     } 
     //set text to show question on screen 
     question.setText(operand1+" "+operators[operator]+" "+operand2); 
    } 
    //random number generator 
    private int getOperand(){ 
     //return operand number 
     return random.nextInt(levelMax[operator][level] - levelMin[operator][level] + 1) 
       + levelMin[operator][level]; 
    } 
    //tell buttons what to do when clicked on 
    @Override 
    public void onClick(View view) { 
     //button clicked 
     if(view.getId()==R.id.enter){ 
      //enter button 
      String answerContent = answerTxt.getText().toString(); 
      if(!answerContent.endsWith("?")) 
      { 
       //answer has been entered, check if correct 
       int enteredAnswer = Integer.parseInt(answerContent.substring(2)); 
       int exScore = getScore(); 
       int exCoincount = getCoincount(); 
       if(enteredAnswer==answer){ 
        //correct - show tick and add one to score and coincount 
        scoreTxt.setText("Score: "+(exScore+1)); 
        response.setImageResource(R.drawable.tick); 
        response.setVisibility(View.VISIBLE); 
        coincountTxt.setText(""+(exCoincount+1)); 
       } 
       else{ 
        //incorrect - show cross and reset score to 0 
        scoreTxt.setText("Score: 0"); 
        response.setImageResource(R.drawable.cross); 
        response.setVisibility(View.VISIBLE); 
       } 
       //show new question 
       chooseQuestion(); 
      } 

     } 
     //if clear button clicked reset answer text to question mark 
     else if(view.getId()==R.id.clear){ 
      //clear button 
      answerTxt.setText("= ?"); 
     } 
     else if(view.getId()==R.id.help_btn){ 
      //help button 
      Intent i = new Intent(PlayGame.this, HowToPlay.class); 
      startActivity(i); 
     } 
     //if number clicked: 
     else { 
      //number button 
      response.setVisibility(View.INVISIBLE); 
      int enteredNum = Integer.parseInt(view.getTag().toString()); 
      //if first number replace question mark 
      if(answerTxt.getText().toString().endsWith("?")) 
       answerTxt.setText("= "+enteredNum); 
      //if subsequent number append to previous 
      else 
       answerTxt.append(""+enteredNum); 
     } 

    } 
    //function to calculate score (used above in 'correct' if statement 
    private int getScore(){ 
     String scoreStr = scoreTxt.getText().toString(); 
     return Integer.parseInt(scoreStr.substring(scoreStr.lastIndexOf(" ")+1)); 
    } 
    //function to calculate number of coins 
    private int getCoincount(){ 
     String coincountStr = coincountTxt.getText().toString(); 
     return Integer.parseInt(coincountStr.substring(coincountStr.lastIndexOf(" ")+1)); 
    } 
} 

我引用的代码的onCreate(),它调用函数chooseQuestion()不远处低于。

+0

请张贴代码的其余 –

+1

“_but我的应用程序现在breaking_” 你会需要比这更具体。它是否编译?你会得到一个异常?错误的活动已打开?什么_exactly_不起作用,以及如何? – csmckelvey

使用处理

new Handler().postDelayed(new Runnable(){ 
     public void run(){ 
      //start activity here 
     } 
    }, 1000); 
+0

感谢您的回复!我越来越无法解决符号处理程序? – Emma

+0

你有进口.... – csmckelvey

试试这个:

for(int i = 0;i <= 20;i++){ 
     if(i==20){ 
     Intent i = new Intent(PlayGame.this, Gamble.class); 
     startActivity(i); 
     break; 
     } else { 
     random = new Random(); 
     chooseQuestion(); 
    } 
} 
+0

请接受适合您的答案。 –

使用线程

new Thread(new Runnable() { 
     @Override 
    public void run() { 
      //enter code here 
    }).start();}