简体   繁体   中英

Implementing 2 Seekbars - Both Modify The Exact Same Text?

I'm attempting to implement a 2nd seekbar but for some odd reason they're both modifying the same text when the seekbars are moved. I've looked the source code up and down and can't identify the issue (I know I'm overlooking something very simple - can anyone spot it?)

JAVA:

     private long rowID; 
     private EditText nameEt;
     private EditText capEt;
     private EditText codeEt;
     private TimePicker timeEt;
     private TextView ssidTextView;
     private TextView sbTv;
     private SeekBar bar;
     private SeekBar bar2;
     private SeekBar bar;
     private SeekBar bar2;
     private TextView textProgress,textAction;
     private TextView textProgress2,textAction2;



       public void onCreate(Bundle savedInstanceState) 
       {
          super.onCreate(savedInstanceState); 
          setContentView(R.layout.add_country);
          bar = (SeekBar)findViewById(R.id.seekBar1);
          bar.setOnSeekBarChangeListener(this);
          bar2 = (SeekBar)findViewById(R.id.seekBar2);
          bar2.setOnSeekBarChangeListener(this);



          textProgress = (TextView)findViewById(R.id.textViewProgress);
          textProgress2 = (TextView)findViewById(R.id.textViewProgress2);

          textAction = (TextView)findViewById(R.id.textViewAction);
          textAction2 = (TextView)findViewById(R.id.textViewAction2);

      }

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

        textProgress.setText(progress+"MBPS");
       }

        public void onProgressChanged2(SeekBar seekBar2, int progress,
                boolean fromUser) {


        textProgress2.setText(progress+"MBPS");

The only function being called here is the first one:

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {    
    textProgress.setText(progress+"MBPS");
}

In this function, you should check which SeekBar is passed and react accordingly:

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {     
    if (seekBar.equals(bar)) {//do something for bar}
    else if (seekBar.equals(bar2)) {//do something for bar2}
}

If your layout is defined in XML (which you should do anyways in a simple app like this), then compare the SeekBars ids:

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {     
    if (seekBar.getId() == R.id.seekbar_1) {//do something for bar}
    else if (seekBar.getId() == R.id.seekbar_2) {//do something for bar2}
}

Here is a complete solution

Activity

public class MainActivity extends Activity implements SeekBar.OnSeekBarChangeListener{

    SeekBar bar1;
    SeekBar bar2;
 TextView textProgress1,textProgress2;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            bar1=(SeekBar)findViewById(R.id.seekBar1);
            bar2=(SeekBar)findViewById(R.id.seekBar2);
            bar1.setOnSeekBarChangeListener(this);
            bar2.setOnSeekBarChangeListener(this);
            textProgress1=(TextView)findViewById(R.id.textView1);
            textProgress2=(TextView)findViewById(R.id.textView2);
        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            switch (seekBar.getId()) {
            case R.id.seekBar1:
                textProgress1.setText(progress +" MBPS");
                break;
          case R.id.seekBar2:
              textProgress2.setText(progress +" MBPS");
                break;
            default:
                break;
            }
        }
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }
 }

activity_main Xml

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center" >

<SeekBar
    android:id="@+id/seekBar1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="39dp" 
    />

<SeekBar
    android:id="@+id/seekBar2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/seekBar1"
    android:layout_marginTop="38dp" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/seekBar1"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="38dp"
    android:text="TextView Progrss 1" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/seekBar1"
    android:layout_marginTop="19dp"
    android:text="TextView Progrss 2"  />

</RelativeLayout>

What is wrong in your code is that the method that is called by the seekbar listener is the one with @override notation above it.

My solution will change the text when the seek is complete , if you need real time change for eg: changing text as soon as it starts the seek,

Implement the following method...................

[ http://developer.android.com/reference/android/widget/SeekBar.OnSeekBarChangeListener.html#onProgressChanged(android.widget.SeekBar , int, boolean)][1] and use the same logic specified below...

Implement the

@override
public void onStopTrackingTouch(SeekBar s){
   switch (arg0.getId()) {
    case R.id.seekBar1:
        setText("ONE");
                    break;
    case R.id.seekBar2:
        setText("TWO");
                    break;  
    default:
        break;        
    }

}

I hope this solves the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM