拖动搜寻栏时如何更新文本视图

时间:2018-07-31 01:16:17

标签: java android listener runnable android-seekbar

在这里您可以看到我正在努力实现的目标

https://imgur.com/a/zEpWI

第一个gif是我现在拥有的,第二个gif是我想要的。

代码有效,但是显示歌曲当前位置的textview仅在我停止拖动时才更新,但是我希望在拖动时更新它。

查看我在链接中发布的gif,以便您了解我要实现的目标。

预先感谢

文斯

updateProgressBar();更改歌曲时被叫

我的代码

private Runnable mUpdateTime = new Runnable() {
@Override
public void run() {
    long songCurrentTime = mediaPlayer.getCurrentPosition();
    long songTotalTime = mediaPlayer.getDuration();

    tvSongCurrentTime.setText(""+utilities.msToTimer(songCurrentTime));
    tvSongTotalTime.setText(""+utilities.msToTimer(songTotalTime));

    int progress = (int)(utilities.getProgressPercentage(songCurrentTime, songTotalTime));
    seekBar.setProgress(progress);

    mHandler.postDelayed(this, 100);
}
};

  public void updateProgressBar(){
     mHandler.postDelayed(mUpdateTime, 100);
}

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean 
fromUser) {

} 

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
    mHandler.removeCallbacks(mUpdateTime);
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
    mHandler.removeCallbacks(mUpdateTime);
    int songTotalTime = mediaPlayer.getDuration() ;
    int currentPosition = utilities.progressToTimer(seekBar.getProgress(), 
    songTotalTime);

    mediaPlayer.seekTo(currentPosition);
    updateProgressBar();
}

3 个答案:

答案 0 :(得分:0)

您应该使用“ onProgressChanged”方法进行操作。像这样:

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean 
    fromUser) {
    if(fromUser)
        //myTextView.setText("" + progress);
        updateProgressBar();
} 

答案 1 :(得分:0)

按照下面的代码。可能会对您有帮助。

final TextView sometextview=findViewById(R.id.someid);//your textview
sometextview.setText("Hello Android");        

        final SeekBar seekbar=(SeekBar) findViewById(R.id.someseekbar);   //your seekbar  
        seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { //listener for your seekbar      

        @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      

            sometextview.setTextSize(progress+"%");//Here is the textview with the progress number.

        }       
    });

答案 2 :(得分:0)

简而言之,您在项目中的代码是错误的方法,请参见下文,将代码剪切并粘贴到正确的方法中...在onProgressChanged方法中执行代码

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean 
fromUser) {

int songTotalTime = mediaPlayer.getDuration() ;
    int currentPosition = utilities.progressToTimer(seekBar.getProgress(), 
    songTotalTime);

    mediaPlayer.seekTo(currentPosition);
    updateProgressBar();

} 

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
    mHandler.removeCallbacks(mUpdateTime);
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
    mHandler.removeCallbacks(mUpdateTime);

}