A few listeners of SeekBars

I have several search buttons on the same view:

barheures = (SeekBar)findViewById(R.id.barheures); // make seekbar object barheures.setOnSeekBarChangeListener(this); // set seekbar listener. // since we are using this class as the listener the class is "this" barminutes = (SeekBar)findViewById(R.id.barminutes); barminutes.setOnSeekBarChangeListener(this); 

And here is the list:

 @Override public void onProgressChanged(SeekBar bar, int progress, boolean fromUser) { // TODO Auto-generated method stub Log.v("", "" + bar); textMinutes.setText("" + progress + "Minute(s)" ); textHours.setText("" + progress + "Heure(s)" ); } 

I want to do something different if the first OR the second bar has moved to the same list (is this good practice?), But how? Here I have an application that does not do what I want ^^

Thank you very much

+4
source share
2 answers

You now have 2 search bars with the following identifiers:

1) barheures - his id: R.id.barheures 2) barminutes - his id: R.id.barminutes

Now in the onProgressChanged method (SeekBar, int progress, boolean fromUser) do the following:

 @Override public void onProgressChanged(SeekBar bar, int progress, boolean fromUser) { // TODO Auto-generated method stub Log.v("", "" + bar); switch (bar.getId()) { case R.id.barheures: textHours.setText("" + progress + "Heure(s)"); break; case R.id.barminutes: textMinutes.setText("" + progress + "Minute(s)"); break; } } 
+17
source

Instead of comparing resource IDs, you can also just check the SeekBar object for equality. Example:

 private SeekBar bar1; private SeekBar bar2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... bar1 = (SeekBar) findViewById(R.id.bar1); bar2 = (SeekBar) findViewById(R.id.bar2); ... } @Override public void onProgressChanged(SeekBar bar, int progress, boolean fromUser) { if (bar.equals(bar1)) { // do something } else if (bar.equals(bar2)) { // do something else } } 

I prefer to do it this way because then you only ever refer to the identifier of each SeekBar resource (in onCreate ).

+4
source

Source: https://habr.com/ru/post/1389162/


All Articles