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

时间:2017-08-02 21:20:09

标签: android

我有一项活动会询问用户一个问题。目前,在用户退出应用程序之前,它会永远提出问题,但我希望它提出20个问题,然后继续进行新的活动。我想如果我这样做了:

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));
    }
}

它会起作用,但我的应用程序现在正在破碎。我是否错误地开设了新活动,如果是这样,我该怎么办?

好的,我对此非常陌生,而且我不知道在回复评论时会发布多少代码,所以这就是全部,对不起。

{{1}}

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

3 个答案:

答案 0 :(得分:2)

使用处理程序

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

答案 1 :(得分:1)

试试这个:

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();
    }
}

答案 2 :(得分:0)

使用线程

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