我如何循环一张code在我的主要活动? [英] How do I loop a piece of code in my main activity?
问题描述
我使用Android的工作室,我想这个循环每隔半秒钟
随机兰特=新的随机();
int值= rand.nextInt(10);
所以无论如何感谢您的时间,如果你能帮助这将是巨大的。 :)
真诚,
伊戈尔
编辑
感谢大家的善良和乐于助人的答案。我会选择最佳答案后不久,我尝试每一个出来。 (不符合我的电脑现在),但再次,谢谢大家。
编辑
对于任何人有类似的问题,我得到它的工作。这里是最后的code。
包sarju7.click;
进口android.os.Handler;
进口android.support.v7.app.ActionBarActivity;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.TextView;
导入了java.util.Random;
公共类MainActivity扩展ActionBarActivity {
随机兰特=新的随机();
处理程序处理程序=新的处理程序();
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
可运行R =新的Runnable(){
公共无效的run(){
int值= rand.nextInt(10);
TextView的T1 =(TextView中)findViewById(R.id.clicker);
t1.setText(Integer.toString(值));
handler.postDelayed(本,400);
}
};
handler.postDelayed(R,400);
}
}
再次感谢大家。你们是最好的。我爱所有的堆栈溢出!
随机兰特=新的随机();
处理程序处理程序=新的处理程序()
可运行R =新的Runnable(){
公共无效的run(){
int值= rand.nextInt(10);
handler.postDelayed(本,500);
}
};
handler.postDelayed(R,500);
I am using Android Studio and I wanted to loop this every half a second
"Random rand = new Random();
int value = rand.nextInt(10);"
So anyway thanks for your time and if you can help that would be great. :)
Sincerely,
Igor
EDIT
Thanks everyone for the kind and helpful answers. I will choose the best answer soon after I try each one out. (Not with my computer right now) But once again, thank you all.
Edit
For anyone having a similar problem I got it to work. Here is the final code.
package sarju7.click;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends ActionBarActivity {
Random rand = new Random();
Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Runnable r = new Runnable() {
public void run() {
int value = rand.nextInt(10);
TextView t1 = (TextView) findViewById(R.id.clicker);
t1.setText(Integer.toString(value));
handler.postDelayed(this, 400);
}
};
handler.postDelayed(r, 400);
}
}
Once again thanks everyone. You guys are the best. I love all of stack overflow!
Random rand = new Random();
Handler handler = new Handler()
Runnable r=new Runnable() {
public void run() {
int value = rand.nextInt(10);
handler.postDelayed(this, 500);
}
};
handler.postDelayed(r, 500);
这篇关于我如何循环一张code在我的主要活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!