关闭活动一旦显示得分

时间:2018-06-15 14:29:36

标签: java android

点击“显示分数”按钮后,我会在分数屏幕上完成一个测验。但是我想知道如何在分数显示几秒后自动关闭测验并返回我的主要活动。

public void showScore(View view) {
    // Calculate the number of correct answers after the "Show score" button is tapped.
    Button button = (Button) findViewById(R.id.score);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Do your other functions //
            int score = calculateScore(answer1, answer2, answer3, answer4);
            // Convert the correct answer-to-question ratio to percentage.
            float percentage = score * 100 / 4;
            // Show the percentage score in a toast

            Toast.makeText(this, "Your score: " + percentage + "%", Toast.LENGTH_LONG).show();

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    Intent i = new Intent(EmotionsActivity.this, ActivitiesActivity.class);
                    startActivity(i);
                    finish(); // closes after score has been shown
                }
            }, 2000); // Set your time here //
        }
    });

错误:

error: no suitable method found for makeText(<anonymous 
OnClickListener>,String,int)
method Toast.makeText(Context,CharSequence,int) is not applicable
(argument mismatch; <anonymous OnClickListener> cannot be converted to 
Context)
method Toast.makeText(Context,int,int) is not applicable
(argument mismatch; <anonymous OnClickListener> cannot be converted to Context)

2 个答案:

答案 0 :(得分:1)

使用https://developer.android.com/reference/android/os/Handler类的postDelayed方法调用活动的finish()。

答案 1 :(得分:0)

在按钮上使用/mnt/testceph# dd if=/dev/zero of=/mnt/testceph/test1 bs=1G count=1 oflag=direct 1+0 records in 1+0 records out 1073741824 bytes (1.1 GB, 1.0 GiB) copied, 23.1742 s, 46.3 MB/s /mnt/testceph# dd if=/dev/zero of=/mnt/testceph/test1 bs=1G count=1 conv=fdatasync 1+0 records in 1+0 records out 1073741824 bytes (1.1 GB, 1.0 GiB) copied, 2.22468 s, 483 MB/s 单击并使用意图更改活动。例如:

postDelayed