首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >一旦显示得分,就关闭活动

一旦显示得分,就关闭活动
EN

Stack Overflow用户
提问于 2018-06-15 14:29:36
回答 2查看 49关注 0票数 0

我有一个小测验,当“显示分数”按钮被点击后,用一个分数屏幕结束。然而,我想知道如何然后自动结束测试,并返回到我的主要活动后,得分已经显示了几秒钟。

代码语言:javascript
复制
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 //
        }
    });

错误:

代码语言:javascript
复制
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)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-15 15:00:08

在按钮上使用postDelayed,单击并使用意图更改活动。示例:

代码语言:javascript
复制
Button button = (Button) findViewById(R.id.score);
 button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Do your other functions //
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
            Intent i = new Intent(ActivityQuiz.this, MainActivity.class);
            startActivity(i);
            finish();
                    }
                }, 5000); // Set your time here //
            }
        });
票数 0
EN

Stack Overflow用户

发布于 2018-06-15 14:31:29

使用postDelayed of https://developer.android.com/reference/android/os/Handler类来对活动调用finish()。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50877617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档