Android-按下按钮需要2-3秒

时间:2018-06-22 02:13:08

标签: java android button

该活动完美加载,并且该活动上的countDownTimer没有问题,但是我按下的4个按钮需要2-3秒才能变得沮丧,并完成分配的任务。

一些值得一提的笔记:

  • 以前的活动没有问题。
  • 只有4个按钮,4个小imageViews和2个textViews。
  • 按下按钮(其中的一些按钮不执行任何操作)会导致countDownTimer冻结,但在后台运行良好,如果您知道我的意思,则可以进行跟踪。

如果有帮助,我可以发布我的代码。

预先感谢我对此真的很陌生

这是我的代码:

public class PlayActivity extends AppCompatActivity {
private boolean isPaused=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_play);

    Random random = new Random();
    int randomQuestion = random.nextInt(3);//number is the number of questions and should probably not be hard coded

    Resources res = getResources();

//Timer initiation
    final TextView timerTextView = findViewById(R.id.timerTextView);
    new CountDownTimer(30000, 1000) {
        public void onTick(long millisUntilFinished) {
            timerTextView.setText("seconds remaining: " + millisUntilFinished / 1000);
            if(isPaused) {
                //transfer timer info to next Activity
                cancel();
                finish();
            }
        }
        public void onFinish() {
           timerTextView.setText("Done!");
           //start gameOverActivity
        }
    }.start();

//declare question array and answer array
    TypedArray answerResources = res.obtainTypedArray(R.array.answers);
    int resId = answerResources.getResourceId(randomQuestion, -1);//gets the ID of the nth string array
    answerResources.recycle();//free
    //if (resId < 0) {QUESTION DOES NOT EXIST.  CHECK strings.xml OR RNG}
    String [] questionAnswers = res.getStringArray(resId);
    String[] questions = res.getStringArray(R.array.question_array);

//declare buttons and textView
    TextView questionTextView = findViewById(R.id.questionTextView);
    Button firstAnswerBtn = findViewById(R.id.firstAnswerBtn);
    Button secondAnswerBtn = findViewById(R.id.secondAnswerBtn);
    Button thirdAnswerBtn = findViewById(R.id.thirdAnswerBtn);
    Button fourthAnswerBtn = findViewById(R.id.fourthAnswerBtn);
    Button[] answerButtons = {firstAnswerBtn,secondAnswerBtn,thirdAnswerBtn,fourthAnswerBtn};

//shuffle the four answers:
    int correctIndex=0;
    for(int i=0; i<4; i++){
        int randomShuffle = random.nextInt(4);
        if(i==correctIndex){//keep track of the correct answer (which always starts at 0)
            correctIndex=randomShuffle;
        }
        else if(randomShuffle==correctIndex){
            correctIndex=i;
        }
        String hold=questionAnswers[i];//quick lil swapperoo
        questionAnswers[i]=questionAnswers[randomShuffle];
        questionAnswers[randomShuffle]=hold;
    }

//set1  the question and the buttons to the randomQuestion
    questionTextView.setText(questions[randomQuestion]);
    answerButtons[0].setText(questionAnswers[0]);
    answerButtons[1].setText(questionAnswers[1]);
    answerButtons[2].setText(questionAnswers[2]);
    answerButtons[3].setText(questionAnswers[3]);


    answerButtons[correctIndex].setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent playIntent = new Intent(PlayActivity.this, PlayActivity.class);
            startActivity(playIntent);
            isPaused=true;
            finish();
        }
    });

}

public void onBackPressed() {
    backButtonOverride();
}

public void backButtonOverride(){
    AlertDialog alertDialog = new AlertDialog.Builder(PlayActivity.this).create();
    alertDialog.setTitle("!");
    alertDialog.setMessage("Return to Main Menu?");
    alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
    alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Ok",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    Intent start = new Intent(PlayActivity.this, MainActivity.class);
                    startActivity(start);
                    finish();
                }
            });
    alertDialog.show();

}

}

1 个答案:

答案 0 :(得分:2)

这是因为您的onClick中有以下几行:

@Override
public void onClick(View v) {
    Intent playIntent = new Intent(PlayActivity.this, PlayActivity.class);
    startActivity(playIntent);
    isPaused=true;
    finish();
}

您正在开始并销毁相同的活动。这可能需要一段时间。这样做不是一个好主意。

只需在同一个Activity上调用一个方法即可更新必要的视图,而不是创建和销毁同一个Activity。