简体   繁体   English

如何在指定时间段内在TextView中显示一些文本?

[英]How to display some text in TextView for a specified amount of time?

I have developed an android application which extracts single line text messages from the server. 我已经开发了一个Android应用程序,可以从服务器提取单行文本消息。 Once a button is clicked, it makes a function call which gets the next message from the server. 单击按钮后,它将进行函数调用,该函数将从服务器获取下一条消息。 Some of those messages are time based, 其中一些消息是基于时间的,

ie those messages have to be displayed in the TextView for a particular amount of time and after that time is elapsed, it should automatically make the function call to get the next message from the server(ie without the button being clicked). 也就是说,这些消息必须在TextView显示特定的时间,并且在这段时间过去之后,它应该自动进行函数调用以从服务器获取下一条消息(即,无需单击按钮)。

Could someone please help me out in achieving this. 有人可以帮我实现这一目标。

I tried using while loop as follows: 我尝试使用while循环,如下所示:

while(!presentTime.equals(expiryTime)){                 
    calculatePresentTym();   //This method calculates the presentTime value
    display.settext(the received instruction);
}

if(presentTime.equals(expiryTime))
    (make the function call)

If I do this, nothing is being displayed till presentTime and expiryTime are equal. 如果我这样做,则直到presentTimeexpiryTime相等之前,什么都不会显示。 Once they are equal, the next instruction is automatically fetched by the function call and is displayed in the TextView. 一旦它们相等,则下一条指令将通过函数调用自动获取,并显示在TextView.

Use aa handler 使用aa处理程序

Handler m_handler;
Runnable m_handlerTask ; 
m_handler = new Handler(); 
@Override
public void run() {
           // do something 
  m_handler.postDelayed(m_handlerTask, 1000);
 }
 };
 m_handlerTask.run(); 

T0 cancel the run T0取消运行

 m_handler.removeCallbacks(m_handlerTask);  // to cancel the run

You can also use a timer but you will have to use runOnUiThread to update ui since timer runs on a different thread. 您还可以使用计时器,但是由于计时器在其他线程上运行,因此您将必须使用runOnUiThread来更新ui。

Timer _t = new Timer();  
 _t.scheduleAtFixedRate( new TimerTask() {
        @Override
        public void run() {
          //do something
           runOnUiThread(new Runnable() //run on ui thread
             {
              public void run() 
              {      
                //update ui
              }
             });

        }
    }, 1000, 1000 ); 

Note: 注意:

gets the next message from the server 从服务器获取下一条消息

Getting the message from server should be done on a background thread. 从服务器获取消息应该在后台线程上完成。

Edit: 编辑:

While copy pasting the initialization part was missing. 复制粘贴时缺少初始化部分。 You have a counter i that is displayed in the textview. 您有一个显示在textview中的计数器i。 The counter increases by 1 every second. 计数器每秒增加1。 When it reaches 100 you cancel the run. 当达到100时,您取消运行。 Modify the below according to your requirements. 根据您的要求修改以下内容。

 public class MainActivity extends Activity {

TextView tv;
Handler m_handler;
Runnable m_handlerTask ; 
int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv = (TextView) findViewById(R.id.textView1);
     m_handler = new Handler();
    m_handlerTask = new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            if(i<=100)
            {
            tv.setText(""+i);
             i++;
            }
            else
            {
                m_handler.removeCallbacks(m_handlerTask);
            }
            m_handler.postDelayed(m_handlerTask, 1000);
        }
    };
    m_handlerTask.run();  
   }
   }  

Use a timer. 使用计时器。 Schedule the timer for repeated interval executions, and after each execution you can get the next text from the server and display the same. 安排计时器以重复执行间隔,每次执行后,您可以从服务器获取下一个文本并显示该文本。

Check the Timer reference scheduleAtFixedRate(TimerTask task, long delay, long period) 检查计时器参考schedule ScheduleAtFixedRate(TimerTask任务,延迟较长,周期长)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM