java - Android - 按钮需要 2-3 秒才能按下

标签 java android button

Activity 加载完美, Activity 上的 countDownTimer 没有问题,但是我的 4 个按钮需要 2-3 秒才能按下并完成分配的任务。

一些有值(value)的笔记可能:

  • 之前的 Activity 没有问题。
  • 只有 4 个按钮、4 个小 ImageView 和 2 个 TextView 。
  • 按下按钮(其中一些什么都不做)会导致 countDownTimer 卡住,但它在后台运行良好,如果您明白我的意思,它就会 catch 来。

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

提前致谢 我是新手

.

这是我的代码:

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 的方法来更新必要的 View ,而不是创建和销毁同一个 Activity。

关于java - Android - 按钮需要 2-3 秒才能按下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50979727/

相关文章:

java - 如何在android中获取文件的权限和属性

android - 如何检查用户是否使用 YouTube api 观看了整个视频

java - Android - 如何找出当前线程的创建位置?

javascript - 评估语句中的按钮文本

android - 当手指 move Android 时,On Touch Listener 不会释放按钮

java - 如何调用EJB

java - 如何将UTF8字符串转换为UTF16

button - Flutter 更改 AppBar 内 OutlineButton 的高度?

java - 如何防止Eclipse在调试中遇到断点时自动打开调试面板

java - 从 HashSet 到 Arraylist 的 Dozer 映射