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

微片

活动完美加载,活动上的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();

}

}
萨加尔

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

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

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章