我有一个小测验,当“显示分数”按钮被点击后,用一个分数屏幕结束。然而,我想知道如何然后自动结束测试,并返回到我的主要活动后,得分已经显示了几秒钟。
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)发布于 2018-06-15 15:00:08
在按钮上使用postDelayed,单击并使用意图更改活动。示例:
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 //
}
});发布于 2018-06-15 14:31:29
使用postDelayed of https://developer.android.com/reference/android/os/Handler类来对活动调用finish()。
https://stackoverflow.com/questions/50877617
复制相似问题