我一直试图在我的基本应用程序中添加几秒延迟到我的活动交换,但每当我尝试时我都会收到错误。我的尝试是基于前一个帖子(How to put some delay in calling an activity from another activity?),但多次都没有成功。有人可以帮忙吗?
这是我的主要活动代码:
package winfield.joe.wind.v1;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.animation.BounceInterpolator;
import android.view.animation.ScaleAnimation;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends Activity {
// public var
private EditText text;
// default function
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//localise this
Toast.makeText(this, "onCreate!!", Toast.LENGTH_LONG).show();
setContentView(R.layout.activity_main);
// (EditText) = typecast
text = (EditText) findViewById(R.id.userInput);
}
//Will be executed by clicking on the calculate button because we assigned "calc" to the "onClick" Property
public void calc(View view) {
bounce(view, 0.95f);
RadioButton toKilometers = (RadioButton) findViewById(R.id.toKilometers);
RadioButton toKnots = (RadioButton) findViewById(R.id.toKnots);
if (text.getText().length() == 0) {
// if the text field is empty show the message "enter a valid number" via toast message
//ATTENTION:localise this
Toast.makeText(this, "enter a valid number", Toast.LENGTH_LONG).show();
} else {
//int userInput = R.string.userInput;
Intent i = new Intent(MainActivity.this, SecondActivity.class);
//putExtra("userInput", userInput);
startActivity(i);
// parse input Value from Text Field
double inputValue = Double.parseDouble(text.getText().toString());
// convert to...
if (toKilometers.isChecked()) {
text.setText(String.valueOf(convertToKM(inputValue)));
// uncheck "to km" Button
toKilometers.setChecked(true);
// check "to knots" Button
toKnots.setChecked(false);
} else { /* if toKnots button isChecked() */
text.setText(String.valueOf(convertToKnots(inputValue)));
// uncheck "to knots" Button
toKnots.setChecked(true);
// check "to km" Button
toKilometers.setChecked(false);
}
}
}
//bounce animation on button on-click
public void bounce(View view, float amount) {
ScaleAnimation anim = new ScaleAnimation(amount, 1f, amount, 1f);
anim.setDuration(750);
anim.setInterpolator(new BounceInterpolator());
view.startAnimation(anim);
}
private double convertToKM(double inputValue) {
// convert knots to km
return (inputValue * 1.8);
}
private double convertToKnots(double inputValue) {
// convert km to knots
return (inputValue * 0.539956803);
}
}
答案 0 :(得分:2)
您可以使用处理程序:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if(...) {
// start here the other Activity
}
else {
// do something else
}
}
}, YOUR_TIME_OUT);
答案 1 :(得分:0)
试试此代码
new Handler().postDelayed(new Runnable() {
@Override public void run() {
//other code here Intent i = new Intent(MainActivity.this,SecondActivity.class);
startActivity(i); //other code here } }, 5000);