【问题标题】:display text when the timer reaches a specific time当计时器到达特定时间时显示文本
【发布时间】:2013-07-04 01:40:56
【问题描述】:

我正在制作一个当你按下按钮时显示随机句子的应用程序,这个过程很完美,我的部分代码是:

 botonok.setOnClickListener(new OnClickListener() 
    {
        public void onClick(View v) 
        {
            Resources res = getResources();
            myString = res.getStringArray(R.array.myArray); 
            String a = myString[rgenerator.nextInt(myString.length)];
            TextView elemento1 = (TextView) findViewById(R.id.elemento1);
            elemento1.setText(a);

...

问题,我需要这些句子逐渐出现,例如,第一句“elemento1”应该在你点击按钮时出现,第二句,半秒后,第三句,一秒后..我有4个句子,如你所见,它们来自一个数组。

感谢您的帮助

【问题讨论】:

    标签: android timer settext chronometer


    【解决方案1】:

    在课后定义一个计时器。

    static volatile int 时间; 定时器 t = new Timer();

    现在像这样编辑你的函数

      botonok.setOnClickListener(new OnClickListener() 
    {
        public void onClick(View v) 
        {
            Resources res = getResources();
            myString = res.getStringArray(R.array.myArray); 
            String a = myString[rgenerator.nextInt(myString.length)];
            time=1;
    
            t.scheduleAtFixedRate(new TimerTask() {
    
            @Override
            public void run() {
    
                runOnUiThread(new Runnable() {
    
    
                    public void run() {
            TextView elemento1 = (TextView) findViewById(R.id.elemento1);
            if(time==0||time==2||time==4||time==6)
            elemento1.setText(a);
            time=time+1;
            }
                    }
    
                });
    
            }
        }, 100, 1000);
    

    【讨论】:

    • 谢谢你,我写了你的代码,它工作!我必须做出一些改变,请阅读我的回答..
    【解决方案2】:

    谢谢你,我刚刚写了你的代码,它工作!我不得不做一些改变,因为有更多的 textView,我的代码是这样的:

    botonok.setOnClickListener(new OnClickListener() 
        {
            public void onClick(View v) 
            {
    time=1; 
                t.scheduleAtFixedRate(new TimerTask() {
                    public void run() {
                        runOnUiThread(new Runnable() {
                            public void run() {
                                if(time==2){
                          Resources res = getResources();
             myString = res.getStringArray(R.array.myArray);    
             String a = myString[rgenerator.nextInt(myString.length)];          
         TextView elemento1 = (TextView) findViewById(R.id.elemento1);
                elemento1.setText(a);
                                }
                                //
                            if(time==4){
                          Resources res = getResources();
              myString = res.getStringArray(R.array.myArray2); 
             String b = myString[rgenerator.nextInt(myString.length)];
         TextView elemento2 = (TextView) findViewById(R.id.elemento2);
                elemento2.setText(b);
    

    // 这里我为 (elemento3 和 elemento4) 编写相同的过程 // 然后我写最后一行:

    }time=time+1; 
                            }
                        });
            }
    }, 100, 1000)   ;
                ;}
    });}}
    

    但是,有一个小错误,第一次触摸按钮,时间是2,4,6,8 但是每次触摸它,它变得更快,我写了一个条件“countTouch++”但它没有不工作:(

    【讨论】:

    • 感谢您的赞赏!如果您能详细说明更快的确切含义,那么我可能会帮助您
    • 嗯,这段代码没问题,但是每次按按钮,时间变短,第一次是(2,4,6,8),第二次是(1/2) , 1, 1 1/2, 2) 以此类推,直到我几乎同时得到它们……我不知道为什么……
    猜你喜欢
    • 2016-09-29
    • 2011-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多