Android SeekBar setOnSeekBarChangeListener

匿名 (未验证) 提交于 2019-12-03 01:41:02

问题:

I'm wondering about the behavior of the android SeekBars OnSeekBarChangeListener. In particular, is the onProgressChanged-method notified only for the first and the last touch on the seekbar?

I'm trying to refresh a TextView that should show the current progress of the SeekBar. But the TextView is just updated on first touch and last touch. Debugging this confirms my assumption that this method is called just twice :( What I would like to have is that the TextView shows every progress change in the SeekBar.

In short: I am searching for a possibility to get a progress listener that is called for every little progress change.

回答1:

I hope this will help you:

final TextView t1=new TextView(this);  t1.setText("Hello Android");             final SeekBar sk=(SeekBar) findViewById(R.id.seekBar1);          sk.setOnSeekBarChangeListener(new 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                t1.setTextSize(progress);         Toast.makeText(getApplicationContext(), String.valueOf(progress),Toast.LENGTH_LONG).show();      }        });             


回答2:

onProgressChanged() should be called on every progress changed, not just on first and last touch (that why you have onStartTrackingTouch() and onStopTrackingTouch() methods).

Make sure that your SeekBar have more than 1 value, that is to say your MAX>=3.

In your onCreate:

 yourSeekBar=(SeekBar) findViewById(R.id.yourSeekBar);  yourSeekBar.setOnSeekBarChangeListener(new yourListener());

Your listener:

private class yourListener implements SeekBar.OnSeekBarChangeListener {          public void onProgressChanged(SeekBar seekBar, int progress,                 boolean fromUser) {                             // Log the progress             Log.d("DEBUG", "Progress is: "+progress);                             //set textView's text             yourTextView.setText(""+progress);         }          public void onStartTrackingTouch(SeekBar seekBar) {}          public void onStopTrackingTouch(SeekBar seekBar) {}      }

Please share some code and the Log results for furter help.



回答3:

onProgressChanged is called every time you move the cursor.

@Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {     textView.setText(String.valueOf(new Integer(progress)));         }

so textView should show the progress and alter always if the seekbar is being moved.



回答4:

Override all methods

    @Override     public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {       }      @Override     public void onStartTrackingTouch(SeekBar arg0) {       }      @Override     public void onStopTrackingTouch(SeekBar arg0) {       }


回答5:

All answers are correct, but you need to convert a long big fat number into a timer first:

    public String toTimer(long milliseconds){     String finalTimerString = "";     String secondsString;     // Convert total duration into time     int hours = (int)( milliseconds / (1000*60*60));     int minutes = (int)(milliseconds % (1000*60*60)) / (1000*60);     int seconds = (int) ((milliseconds % (1000*60*60)) % (1000*60) / 1000);     // Add hours if there     if(hours > 0){         finalTimerString = hours + ":";     }     // Prepending 0 to seconds if it is one digit     if(seconds 

And this is how you use it:

@Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { textView.setText(String.format("%s", toTimer(progress)));         }


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!