Java 如何在应用程序中的活动之间添加几秒钟的延迟?
我一直在尝试在我的基本应用程序中为我的活动交换添加几秒钟的延迟,但每次尝试都会出错。我基于前面的线程()进行尝试,但多次失败。有人能帮忙吗 以下是我的主要活动代码:Java 如何在应用程序中的活动之间添加几秒钟的延迟?,java,android,android-intent,android-activity,delay,Java,Android,Android Intent,Android Activity,Delay,我一直在尝试在我的基本应用程序中为我的活动交换添加几秒钟的延迟,但每次尝试都会出错。我基于前面的线程()进行尝试,但多次失败。有人能帮忙吗 以下是我的主要活动代码: package winfield.joe.wind.v1; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.View; import android.view.anim
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);
}
}
您可以使用处理程序:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if(...) {
// start here the other Activity
}
else {
// do something else
}
}
}, YOUR_TIME_OUT);
试试这个代码
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);
您希望在哪一点引入延迟?您想添加延迟以便动画完成还是在其他地方?动画只需要一秒钟左右,我只是想让延迟持续5秒,让用户有机会读取结果(显示在屏幕上的“calc”),然后转到第二个活动。自动取款机一切正常,只需增加一个延迟。希望一切都会好起来。啊,好的。在这种情况下,handler.postDelayed()(下面的答案)就是您想要的。好的,谢谢!谢谢你的回答。我一直在研究Handler,但当我尝试实现它时,它会导致很多错误。我试过你的方法,但我得到了大约5个错误,这似乎是因为我的代码布局。还有别的办法吗?谢谢。你能在这里展示你在处理程序中使用的代码吗?是的。我从另一个线程中尝试了以下内容(请参阅本文的描述)<代码>新处理程序().postDelayed(新Runnable(){@Override public void calc(){//此处的其他代码Intent i=新Intent(MainActivity.this,SecondActivity.class);startActivity(i);//此处的其他代码},5000)事实上,这就是你在上面所做的!对不起,我没有马上发现。但这会导致很多错误。实际上,在JoeInfield91发布的上述代码中没有run方法