android重新运行处理程序postdelayed

时间:2016-04-13 00:37:12

标签: android handler postdelayed

我正在尝试使用postdelayed处理程序创建一个更改内容四次(每次更换一次)的textview,该处理程序将在最后停止并在单击按钮后重播相同的序列。

由于某种原因,我无法理解,第二次,只播放了系列的第一部,而剩余的内容未显示。

代码示例如下

Handler handler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) 
{   
    super.onCreate(savedInstanceState);
    setContentView(R.layout.page);

    delay1();

    button1.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v) {  delay1(); }});       

}

private void  delay1() 
{    
    final TextView textvie = (TextView) findViewById( R.id.textvi );

    if(apple >= 1)
    {    
         handler.postDelayed(new Runnable() { 
         public void run() {  textvie.setText(" "); 
         if(apple == 1 ) { stop_n_display(); to_stop = 1; } } 
         }, 700 );   
    }
    if(apple > 0)
    {       
         handler.postDelayed(new Runnable() { 
         public void run() {   textvie.setText("1 ";   } 
          }, 710);    
    }
    if(apple >= 2)
    {
        handler.postDelayed(new Runnable() { 
        public void run() {  textvie.setText(" "); 
            if(apple == 2 ) { stop_n_display(); to_stop = 1; } } 
           }, 1400 );    
    }

    if((apple > 1) & (to_stop != 1)) 
    {          
         handler.postDelayed(new Runnable() { 
         public void run() {    textvie.setText("2 "; } 
         }, 1410); 
    }
    if(apple >= 3)
    {    
         handler.postDelayed(new Runnable() { 
         public void run() { textvie.setText(" "); 
            if(apple == 3 ) {  stop_n_display(); to_stop = 1;} } 
            }, 2100 );   
    }            
    if((apple > 2) & (to_stop != 1)) 
    {               
         handler.postDelayed(new Runnable() { 
         public void run() { textvie.setText("3 "; } 
            }, 2110); 
    }
    if(apple >= 4)
    {  
         handler.postDelayed(new Runnable() { 
         public void run() { textvie.setText(" "); 
            if(apple == 4 ) {stop_n_display(); to_stop = 1;} } 
         }, 2800 );    
    }    
    if((apple > 3) & (to_stop != 1)) 
    {                       
         handler.postDelayed(new Runnable() { 
         public void run() { textvie.setText("4 "; } 
         }, 2820); 
    }
}

一直试图弄清楚好几天,但似乎无法找到问题。请帮忙。我将不胜感激任何直接修复或上述方法的任何替代方案。提前谢谢。

2 个答案:

答案 0 :(得分:0)

这是你的问题

if(apple > 0){       
if(apple >= 1){   
if(apple >= 2){  
if(apple >= 3){  
if(apple >= 4){     

只需致电

if(apple >0){  

不要致电

if(apple >= 1){   
if(apple >= 2){  
if(apple >= 3){  
if(apple >= 4){ 

因为苹果> 0。它总是> 2,3,4,

修正:

if(apple > 0 && apple <1){

}

if(apple >= 1 && apple < 2){

}

 if(apple >= 2 && apple < 3){

}

if(apple >= 3 && apple < 4){

}

答案 1 :(得分:0)

倒计时数字的示例代码。拨打按钮的dealy1()方法onClick

int apple;
private void delay1() {
    apple = 0;//sequence start from 0
    final TextView textvie = (TextView) findViewById(R.id.textvi);
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            textvie.setText(String.valueOf(apple++));//series 0 1 2 3 4
            if (apple != 5)//stop counting when apple reaches 5
                handler.postDelayed(this, 1000);//one sec delay
        }
    }, 1000);//one sec delay
}
相关问题