Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Android中延迟进程/增量_Java_Android - Fatal编程技术网

Java 如何在Android中延迟进程/增量

Java 如何在Android中延迟进程/增量,java,android,Java,Android,我正在创建一个android问答应用程序,我想做的是知道如何延迟这个过程或增量 我想在它添加另一个问题之前延迟2秒钟 这是我的代码,我有qid++在那里,它将在用户回答问题后添加问题。我想推迟2秒钟 包com.example.quick import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.app.Activity; import android.content.In

我正在创建一个android问答应用程序,我想做的是知道如何延迟这个过程或增量

我想在它添加另一个问题之前延迟2秒钟

这是我的代码,我有qid++在那里,它将在用户回答问题后添加问题。我想推迟2秒钟

包com.example.quick

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.TimeUnit;

public class CS_Assembly_Easy extends Activity{


    ArrayList<Question> quesList;
    ArrayList<Question> toSelectFrom; // <--- HERE
    int score = 0;
    int qid = 0;
    int lives = 5;
    int round = 1;
    int timer;




    Question currentQ;
    TextView txtQuestion, times, scored, livess, rounds;
    Button button1, button2, button3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cs_assembly_easy);
        CS_Assembly_QuizHelper db = new CS_Assembly_QuizHelper(this);  // my question bank class
        quesList = db.getAllQuestions();
        toSelectFrom = new ArrayList<Question>(); // <--- HERE
        toSelectFrom.addAll(quesList); // <--- HERE
        Random random = new Random();// this will fetch all quetonall questions
        currentQ = toSelectFrom.get( random.nextInt(toSelectFrom.size())); // the current question <-- edited here too.
        toSelectFrom.remove(toSelectFrom.indexOf(currentQ)); // <--- HERE
        txtQuestion = (TextView) findViewById(R.id.txtQuestion);
        // the textview in which the question will be displayed
        // the three buttons,
        // the idea is to set the text of three buttons with the options from question bank
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);

        livess = (TextView) findViewById(R.id.livess);
        // the textview in which  will be displayed
        scored = (TextView) findViewById(R.id.score);
        // the timer
        times = (TextView) findViewById(R.id.timers);
        rounds = (TextView) findViewById(R.id.round);
        // method which will set the things up for our game
        setQuestionView();
        times.setText("00:02:00");
        // A timer of 60 seconds to play for, with an interval of 1 second (1000 milliseconds)
        CounterClass timer = new CounterClass(60000, 1000);
        timer.start();
        // button click listeners
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // passing the button text to other method
                // to check whether the anser is correct or not
                // same for all three buttons
                getAnswer(button1.getText().toString());
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getAnswer(button2.getText().toString());
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getAnswer(button3.getText().toString());
            }
        });
    }

    public void getAnswer(String AnswerString) {
        if (currentQ.getANSWER().equals(AnswerString)) {
            // if conditions matches increase the int (score) by 1
            // and set the text of the score view
            score++;
            scored.setText("  Score : " + score);



            LayoutInflater inflater = getLayoutInflater();
            // Inflate the Layout
            View layout = inflater.inflate(R.layout.correct,
                    (ViewGroup) findViewById(R.id.custom_toast_layout));

            TextView text = (TextView) layout.findViewById(R.id.textToShow);
            // Set the Text to show in TextView
            text.setText("CORRECT");
            text.setTextSize(25);

            Toast toast = new Toast(getApplicationContext());
            toast.setGravity(Gravity.BOTTOM, 0, 50);
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setView(layout);
            toast.show();





        } else if(lives > 1){
            lives--;
            livess.setText("Lives: " + lives);
            LayoutInflater inflater = getLayoutInflater();
            // Inflate the Layout
            View layout = inflater.inflate(R.layout.wrong,
                    (ViewGroup) findViewById(R.id.custom_toast_layout));

            TextView text = (TextView) layout.findViewById(R.id.textToShow);
            // Set the Text to show in TextView

            text.setText("WRONG");
            text.setTextSize(25);

            Toast toast = new Toast(getApplicationContext());
            toast.setGravity(Gravity.BOTTOM, 0, 50);
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setView(layout);
            toast.show();


        }

        else {
            Intent intent = new Intent(CS_Assembly_Easy.this,
                    ResultActivity_Assembly_Easy.class);
            Bundle b = new Bundle();
            b.putInt("score", score); // Your score
            intent.putExtras(b); // Put your score to your next
            startActivity(intent);
            finish();

        }

        {


        }
        if (qid < 10) {
            // if questions are not over then do this
            Random random = new Random();
            currentQ = toSelectFrom.get(random.nextInt(toSelectFrom.size())); // <<--- HERE
            toSelectFrom.remove(toSelectFrom.indexOf(currentQ)); // <<--- AND HERE
            setQuestionView();
            round++;
            rounds.setText("   Question:" + round + "/10");






        } else {
            // if over do this
            Intent intent = new Intent(CS_Assembly_Easy.this,
                    ResultActivity_Assembly_Easy.class);
            Bundle b = new Bundle();
            b.putInt("score", score); // Your score
            intent.putExtras(b); // Put your score to your next
            startActivity(intent);
            finish();
        }
    }
    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    @SuppressLint("NewApi")
    public class CounterClass extends CountDownTimer {
        public CounterClass(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
            // TODO Auto-generated constructor stub
        }
        @Override
        public void onFinish() {
            times.setText("Time is up");
        }
        @Override
        public void onTick(long millisUntilFinished) {
            // TODO Auto-generated method stub
            long millis = millisUntilFinished;
            String hms = String.format(
                    "%02d:%02d:%02d",
                    TimeUnit.MILLISECONDS.toHours(millis),
                    TimeUnit.MILLISECONDS.toMinutes(millis)
                            - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS
                            .toHours(millis)),
                    TimeUnit.MILLISECONDS.toSeconds(millis)
                            - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS
                            .toMinutes(millis)));
            System.out.println(hms);
            times.setText(hms);
        }
    }
    private void setQuestionView() {
        // the method which will put all things together

        txtQuestion.setText(currentQ.getQUESTION());
        button1.setText(currentQ.getOPTA());
        button2.setText(currentQ.getOPTB());
        button3.setText(currentQ.getOPTC());
        qid++;
    }

    @Override
    public void onBackPressed() {
        return;
    }

}
导入android.annotation.SuppressLint;
导入android.annotation.TargetApi;
导入android.app.Activity;
导入android.content.Intent;
导入android.os.Build;
导入android.os.Bundle;
导入android.os.CountDownTimer;
导入android.view.Gravity;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.Button;
导入android.widget.TextView;
导入android.widget.Toast;
导入java.util.ArrayList;
导入java.util.Random;
导入java.util.concurrent.TimeUnit;
公共类CS_Assembly_轻松扩展活动{
ArrayList quesList;
ArrayList toSelectFrom;//请参见此链接

您可以使用:

handler=new Handler();
Runnable r=new Runnable() {
    public void run() {
        //your code here             
    }
};
handler.postDelayed(r, 2000); // waiting for 2s

您可以尝试使用调度
Runnable

将应该延迟的代码放入
Runnable.run()方法中,如下所示:

Handler h = new Handler();
        h.postDelayed(new Runnable() {
            @Override
            public void run() {
                // TODO your code here
            }
        }, 2000L); 

只有在后台线程中执行该操作时,才会出现这种情况。否则,它会阻塞非常糟糕的UI。我应该在哪里声明处理程序h=new Handler();h.postDelayed(new Runnable(){以及我应该在哪里声明@Override public void run(){//TODO your code here},2000L);