[英]Close Activity once score has been shown
单击“显示分数”按钮后,我将完成一个分数屏幕测验。 但是,我想知道如何在分数显示几秒钟后自动关闭测验并返回我的主要活动。
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)
使用https://developer.android.com/reference/android/os/Handler类的postDelayed方法在活动上调用finish()。
在按钮单击上使用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 //
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.