2016-06-09 73 views
-1

我試圖用一個按鈕來讓我的搜索欄慢慢增加& & ||遞減到一定int值(也許我只能用雙做到這一點?),讓我們說搜索條的進度從0開始,我想隨着時間約5秒期間遞增到50。這在Android中可能嗎?遞增/遞減搜索欄慢慢

// ON SEEKBAR CHANGE 
    fill.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 

     @Override 
     public void onStopTrackingTouch(SeekBar seekBar) { 
      // TODO Auto-generated method stub 
     } 

     @Override 
     public void onStartTrackingTouch(SeekBar seekBar) { 
      // TODO Auto-generated method stub 
     } 

     @Override 
     public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) { 
      // TODO Auto-generated method stub 

      fillProgress = String.valueOf(progress); 
      fillView.setText(fillProgress); 

     } 
    }); 

    centerDistribution.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 

      // TOAST: ARE YOU SURE YOU WANT TO fill bucket? 
      // IF YES 
      // INCREMENT SEEKBAR SLOWLY 

    } 
    }); 

回答

0

我從來沒有親自使用它用於此目的,但你可以使用AsyncTask,以確保您的UI會保持在移動過程中響應。從本質上講,你將使用AsyncTask像你更新進度條,但不是一個進度條,你會被更新,而不是seekbar的價值。 You'll need to override the entire class,但麪包,黃油會在這裏:

private class AsyncTaskSeekbar extends AsyncTask<String, String, String> 
{  
    @Override 
    protected void doInBackground(String... params) 
    { 
    try 
    { 
    int time = 5000;   //5000 ms = 5 seconds 
    int incTime = 5000/50; //we want it to get to 50 after 5 seconds 100ms per Seekbar unit 

    for(int i = 0; i < time; i += incTime) 
    { 
     Thread.sleep(incTime); 
     publishProgress(); // Calls onProgressUpdate() 
    } 
    } catch (InterruptedException e) { 
    e.printStackTrace(); 
    } catch (Exception e) { 
    e.printStackTrace(); 
    } 
    } 

    @Override 
    protected void onProgressUpdate() 
    { 
    seekBar.setProgess(seekBar.getProgess() + 1); 
    } 
} 

當然,你就必須做一些事情像初始化AsyncTaskSeekbar,在值傳遞動態等,但是這應該給你的如何是個好主意開始。