简体   繁体   中英

How to sum progress values of two seekbars in text view?

I have written this code. I want to sum the progress values of two seekbars in a text view. I have tried several time but it doesn't work. I want to some both values in a variable. Where I can use the variable in percentage etc.

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);  seekBar1=(SeekBar)findViewById(R.id.moveOnSeekBar1);    seekBar2=(SeekBar)findViewById(R.id.moveOnSeekBar2);    screenView1=(TextView)findViewById(R.id.screenView1);   screenView2=(TextView)findViewById(R.id.screenView2);   screenViewTotal=(TextView)findViewById(R.id.screenViewTotal);
    final int total;
    total=progress1Value+progress2Value;
    seekBar1.setMax(20);    seekBar1.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
            @Override
            public void onProgressChanged(SeekBar p1, int progress1, boolean p3)
            {   screenView1.setText(Integer.toString(progress1));
                progress1Value=progress1;   screenViewTotal.setText(String.valueOf(total));
            }
            @Override
            public void onStartTrackingTouch(SeekBar p1)
            {
            }
            @Override
            public void onStopTrackingTouch(SeekBar p1)
            {
                // TODO: Implement this method
            }
        });
    seekBar2.setMax(20);    seekBar2.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
            @Override
            public void onProgressChanged(SeekBar p1, int progress2, boolean p3)
            {   screenView2.setText(Integer.toString(progress2));
                progress1Value=progress2;   screenViewTotal.setText(String.valueOf(total));
            }
            @Override
            public void onStartTrackingTouch(SeekBar p1)
            {
            }
            @Override
            public void onStopTrackingTouch(SeekBar p1)
            {
                // TODO: Implement this method
            }
        });
}

I have taken your code and modified it. Create two fields, progress1 and progress2 . Then as the user interacts with the seekbars, update the correct value. Then call updateTotal() , which will calculate your total so you can use it to display the information.

private int progress1 = 0;
private int progress2 = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // ...
    seekBar1.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
            @Override
            public void onProgressChanged(SeekBar p1, int progress1, boolean p3) {   
                this.progress1 = progress1;
                updateTotal();
            }
            // ...
        });

    // ...
    seekBar2.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
            @Override
            public void onProgressChanged(SeekBar p1, int progress2, boolean p3) {   
                this.progress2 = progress2;
                updateTotal();
            }

            // ...
        });
}

private void updateTotal() {
    int total = progress1 + progress2;
    screenViewTotal.setText(String.valueOf(total));

    // do whatever else you want with the total.
}

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