Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 每5秒用字符串数组中的每个字符串更新TextView_Android_Arrays_String_Countdowntimer - Fatal编程技术网

Android 每5秒用字符串数组中的每个字符串更新TextView

Android 每5秒用字符串数组中的每个字符串更新TextView,android,arrays,string,countdowntimer,Android,Arrays,String,Countdowntimer,我有一个TextView,我想每隔5秒钟用字符串数组中的每个字符串更新TextView 这是我试过的代码。它始终仅显示字符串数组中的最后一个字符串 TextView display; EditText caption; Thread thread; String blinks; String[] wc; private CountDownTimer timer; @Override protected void onCreate(Bundle savedInstanceState) {

我有一个
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();
}
TextView显示;
编辑文本标题;
螺纹;
字符串闪烁;
字符串[]wc;
私人倒计时;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display=(TextView)findViewById(R.id.display);
caption=(EditText)findViewById(R.id.caption);
计时器=新的倒计时计时器(5000,20){
@凌驾
公共void onTick(长毫秒未完成){
String[]wc={“The”,“Qucik”,“Brown”,“fox”,“Jumped”};
对于(int j=0;j
试着在android中使用handler,我想你是这样的:

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);
String[]wc={“The”,“Qucik”,“Brown”,“fox”,“hopped”};
int j=0;
Handler=newhandler();
Runnable runit=new Runnable(){
@凌驾
public void run(){
试一试{
对于(;j
  • 问题: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;  
}

如果答案有用。请投票:)


在这里使用Handler要简单得多。你能使它无限循环吗?要使它无限循环,只需重置计数器,如:
display.setText(wc[i++]);如果(i==wc.length){i=0;}handler.postDelayed(这个,5000)
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);
                    }
                }
            });