每 5 秒用字符串数组中的每个字符串更新 TextView
Update TextView with each string in String-array every 5 seconds
我有一个 TextView
,我想每 5 秒用我的字符串数组中的每个字符串更新一次 TextView
。
这是我试过的代码。它始终只显示字符串数组中的最后一个字符串。
TextView display;
EditText caption;
Thread thread;
String blinks;
String[] wc;
private CountDownTimer timer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display = (TextView) findViewById(R.id.display);
caption = (EditText) findViewById(R.id.caption);
timer = new CountDownTimer(5000, 20) {
@Override
public void onTick(long millisUntilFinished) {
String[] wc = {"The","Qucik", "Brown","fox","Jumped"};
for (int j = 0; j < wc.length; j++) {
blinks = wc[j];
final String[] titles = {"" + blinks + ""};
for (int i = 0; i < titles.length; i++) {
display.setText(titles[i]);
}
}
}
@Override
public void onFinish() {
try{
yourMethod();
}catch(Exception e){
}
}
}.start();
}
尝试在 android 中与处理程序一起玩,我认为您正在使用这样的东西:
String[] wc = {"The","Qucik", "Brown","fox","Jumped"};
int j = 0;
Handler handler = new Handler();
Runnable runit = new Runnable() {
@Override
public void run() {
try{
for (; j < wc.length; j++) {
blinks = wc[j];
final String[] titles = {"" + blinks + ""};
for (int i = 0; i < titles.length; i++) {
display.setText(titles[i]);
}
handler.postDelayed(this,5000);
}
}
catch (Exception e) {
// handle exception
}
}
};
handler.postDelayed(runit, 5000);
final String[] wc = {"The", "Qucik", "Brown", "fox", "Jumped"};
final android.os.Handler handler = new android.os.Handler();
handler.post(new Runnable() {
int i = 0;
@Override
public void run() {
display.setText(wc[i]);
i++;
if (i == wc.length) {
handler.removeCallbacks(this);
} else {
//5 sec
handler.postDelayed(this, 1000 * 5);
}
}
});
- 问题:for 循环的最后一次迭代(在 j 上)将文本设置为最后一个字符串。
您需要确保每个报价只调用一次 TextView.setText()
。然后您可以采用您的方法 (CountDownTimer
) 或 (Handler
)。使用您的方法,这将类似于:
int counter = 0; // field member, NOT local variable
@Override
public void onTick(long millisUntilFinished) {
display.setText("{" + wc[counter++] + "}");
if (counter == 5) counter = 0;
}
如果回答有用。请投票:)
final String[] gritArray = getResources().getStringArray(R.array.gritInformation);
//creating new handler allows send and process message
final Handler gritInfoHanlder = new Handler();
//adding runnable to message queque
gritInfoHanlder.post(new Runnable() {
//initializing array position
int tipPosition = 0;
@Override
public void run() {
//set number of tip(randon/another way)
//setting array to textview
gritInfo.setText(gritArray[tipPosition]);
tipPosition++;
//if array exist its length remove callback
if (tipPosition == gritArray.length) {
gritInfoHanlder.removeCallbacks(this);
//delayed handler 10 sec
} else {
gritInfoHanlder.postDelayed(this, 1000 * 10);
}
}
});
我有一个 TextView
,我想每 5 秒用我的字符串数组中的每个字符串更新一次 TextView
。
这是我试过的代码。它始终只显示字符串数组中的最后一个字符串。
TextView display;
EditText caption;
Thread thread;
String blinks;
String[] wc;
private CountDownTimer timer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display = (TextView) findViewById(R.id.display);
caption = (EditText) findViewById(R.id.caption);
timer = new CountDownTimer(5000, 20) {
@Override
public void onTick(long millisUntilFinished) {
String[] wc = {"The","Qucik", "Brown","fox","Jumped"};
for (int j = 0; j < wc.length; j++) {
blinks = wc[j];
final String[] titles = {"" + blinks + ""};
for (int i = 0; i < titles.length; i++) {
display.setText(titles[i]);
}
}
}
@Override
public void onFinish() {
try{
yourMethod();
}catch(Exception e){
}
}
}.start();
}
尝试在 android 中与处理程序一起玩,我认为您正在使用这样的东西:
String[] wc = {"The","Qucik", "Brown","fox","Jumped"};
int j = 0;
Handler handler = new Handler();
Runnable runit = new Runnable() {
@Override
public void run() {
try{
for (; j < wc.length; j++) {
blinks = wc[j];
final String[] titles = {"" + blinks + ""};
for (int i = 0; i < titles.length; i++) {
display.setText(titles[i]);
}
handler.postDelayed(this,5000);
}
}
catch (Exception e) {
// handle exception
}
}
};
handler.postDelayed(runit, 5000);
final String[] wc = {"The", "Qucik", "Brown", "fox", "Jumped"};
final android.os.Handler handler = new android.os.Handler();
handler.post(new Runnable() {
int i = 0;
@Override
public void run() {
display.setText(wc[i]);
i++;
if (i == wc.length) {
handler.removeCallbacks(this);
} else {
//5 sec
handler.postDelayed(this, 1000 * 5);
}
}
});
- 问题:for 循环的最后一次迭代(在 j 上)将文本设置为最后一个字符串。
您需要确保每个报价只调用一次 TextView.setText()
。然后您可以采用您的方法 (CountDownTimer
) 或 Handler
)。使用您的方法,这将类似于:
int counter = 0; // field member, NOT local variable
@Override
public void onTick(long millisUntilFinished) {
display.setText("{" + wc[counter++] + "}");
if (counter == 5) counter = 0;
}
如果回答有用。请投票:)
final String[] gritArray = getResources().getStringArray(R.array.gritInformation);
//creating new handler allows send and process message
final Handler gritInfoHanlder = new Handler();
//adding runnable to message queque
gritInfoHanlder.post(new Runnable() {
//initializing array position
int tipPosition = 0;
@Override
public void run() {
//set number of tip(randon/another way)
//setting array to textview
gritInfo.setText(gritArray[tipPosition]);
tipPosition++;
//if array exist its length remove callback
if (tipPosition == gritArray.length) {
gritInfoHanlder.removeCallbacks(this);
//delayed handler 10 sec
} else {
gritInfoHanlder.postDelayed(this, 1000 * 10);
}
}
});