Android Studio Java:如何在循环期间强制重新绘制/显示文本框?
我有下面的代码,我希望屏幕上的数字能达到300万 它编译并运行,最后在模拟器上显示3000000。我的问题是如何在循环过程中强制重新绘制/显示文本框Android Studio Java:如何在循环期间强制重新绘制/显示文本框?,java,android-studio,Java,Android Studio,我有下面的代码,我希望屏幕上的数字能达到300万 它编译并运行,最后在模拟器上显示3000000。我的问题是如何在循环过程中强制重新绘制/显示文本框 /** Called when the activity has become visible. */ @Override protected void onResume() { super.onResume(); Log.d(msg, "The onResume() event");
/** Called when the activity has become visible. */
@Override
protected void onResume() {
super.onResume();
Log.d(msg, "The onResume() event");
TextView textbox1=(TextView)findViewById(R.id.TextView1);
for(double l=0; l<=3000000; l++){
textbox1.setText("" + l);
}
}
/**在活动可见时调用*/
@凌驾
受保护的void onResume(){
super.onResume();
Log.d(msg,“onResume()事件”);
TextView textbox1=(TextView)findViewById(R.id.TextView1);
对于(双l=0;l视图仅在onResume
完成运行后禁用
您可能希望在textview中设置文本,更新活动中的某些状态(如int字段),并注册一些代码以在一段时间后运行并递增。请查看使用或其他选项延迟代码
下面是一个关于Handler
的快速而肮脏的示例
final long DELAY_MILLIS = 50;
final Handler handler = new Handler();
int num = 0;
final Runnable runnable = new Runnable() {
public void run() {
if (num >= 3000000) return;
textbox1.setText("" + num);
num++;
// re-register ourself to run in DELAY_MILLIS;
handler.postDelayed(runnable, DELAY_MILLIS);
}
};
TextView textbox1;
protected void onResume() {
// more efficient to look this up once
this.textbox1 = (TextView)findViewById(R.id.TextView1);
runnable.run(); // will register itself to re-run
}
只有在onResume
完成运行后,该视图才会被禁用
您可能希望在textview中设置文本,更新活动中的某些状态(如int字段),并注册一些代码以在一段时间后运行并递增。请查看使用或其他选项延迟代码
下面是一个关于Handler
的快速而肮脏的示例
final long DELAY_MILLIS = 50;
final Handler handler = new Handler();
int num = 0;
final Runnable runnable = new Runnable() {
public void run() {
if (num >= 3000000) return;
textbox1.setText("" + num);
num++;
// re-register ourself to run in DELAY_MILLIS;
handler.postDelayed(runnable, DELAY_MILLIS);
}
};
TextView textbox1;
protected void onResume() {
// more efficient to look this up once
this.textbox1 = (TextView)findViewById(R.id.TextView1);
runnable.run(); // will register itself to re-run
}
根据答案,在TextView
中显示增量值的最佳方法是使用ValueAnimator
public void animateTextView(int initialValue, int finalValue, final TextView textview) {
ValueAnimator valueAnimator = ValueAnimator.ofInt(initialValue, finalValue);
valueAnimator.setDuration(1500);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
textview.setText(valueAnimator.getAnimatedValue().toString());
}
});
valueAnimator.start();
}
根据答案,在TextView
中显示增量值的最佳方法是使用ValueAnimator
public void animateTextView(int initialValue, int finalValue, final TextView textview) {
ValueAnimator valueAnimator = ValueAnimator.ofInt(initialValue, finalValue);
valueAnimator.setDuration(1500);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
textview.setText(valueAnimator.getAnimatedValue().toString());
}
});
valueAnimator.start();
}
你能解释一下吗?你需要做什么?描述你期望的输出我希望看到文本框中的值明显向上计数。你能解释一下吗?你需要做什么?描述你期望的输出我希望看到文本框中的值明显向上计数。