如何求和两个SeekBars的两个值并在textview中打印出来?

时间:2018-08-04 10:51:18

标签: java android

我只想对两个搜索栏的两个值求和以求和并将其打印在文本视图中。

public class MainActivity extends Activity 
{
    int x;
    int y;

我创建了两个整数x和y的变量。在seekBar1中,我想将seekbar的值分配给x。并且在seekBar2中,我想将seekbar的值分配给y。

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        SeekBar seekBar1=(SeekBar)findViewById(R.id.mainSeekBar1);
        SeekBar seekBar2=(SeekBar)findViewById(R.id.mainSeekBar2);
        final TextView mainTextView=(TextView)findViewById(R.id.mainTextView);
        final TextView totalView=(TextView)findViewById(R.id.mainTextView2);
        seekBar1.setMax(100);
        seekBar1.setProgress(10);   seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
                @Override
                public void onProgressChanged(SeekBar p1, int progress, boolean p3)
                {   mainTextView.setText(String.valueOf(progress));
                    x = progress;
                }
                //.........
            });
        seekBar2.setMax(20);
        seekBar2.setProgress(10);   seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
                @Override
                public void onProgressChanged(SeekBar p1, int progress, boolean p3)
                {   mainTextView.setText(String.valueOf(progress));
                    y = progress;
                }
                //...........
            });
        int total=x * y;
        totalView.setText(String.valueOf(total));
    }
  • 此处为x分配的seekBar1的值

  • seekBar2的值分配给y

我只想将x和y的值相乘/求和,然后在textview中打印出来。

1 个答案:

答案 0 :(得分:1)

尽管不清楚您想要什么:
像在x和y之后一样声明变量总和:

int sum = 0;

然后:

    seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
        @Override
        public void onProgressChanged(SeekBar p1, int progress, boolean p3)
        { 
            mainTextView.setText(String.valueOf(progress));
            x=progress;
            sum = x + y;
            totalView.setText(String.valueOf(sum));            }
    });

    seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
        @Override
        public void onProgressChanged(SeekBar p1, int progress, boolean p3)
        {
            mainTextView.setText(String.valueOf(progress));
            y=progress;
            sum = x + y;
            totalView.setText(String.valueOf(sum));            }
    });