Android Java 使用 setOnSeekBarChangeListener 或 Button 的两个 SeekBar 的值总和

Android Java Sum of values from Two SeekBar's using setOnSeekBarChangeListener or Button

在 Android 中是新的 我正在尝试使用 setOnSeekBarChangeListener 或使用 Button 简单地添加(求和)来自两个 SeekBar 的调整值。 下面的项目工作正常,但总和仍然为 0。我找不到解决方案。谢谢你的帮助。通常,我使用 Spring Boot 和 Vaadin,它是如此简单:)


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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView textViewMain;
        Button button;
        final TextView textViewMain_ = findViewById(R.id.textViewMain);

        Button buttonSum = findViewById(R.id.button);
        SeekBar seekBar = findViewById(R.id.seekBar);
        final TextView textViewSeekBar = findViewById(R.id.textView2);
        SeekBar seekBar2 = findViewById(R.id.seekBar2);
        final TextView textViewSeekBar2 = findViewById(R.id.textView3);
        seekBar.setMax(600);
        seekBar.setProgress(5);
        seekBar2.setMax(900);
        seekBar2.setProgress(5);


        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            public int progress1 = 0;

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress1, boolean fromUser) {
                this.progress1 = progress1;
                // getUpdateTotal();
                //textViewSeekBar.setText(Integer.parseInt(String.valueOf(seekBar)));//todo not work
                textViewSeekBar.setText(Integer.toString(progress1));
                Log.i("Seeker1: ", Integer.toString(progress1));

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            public int progress2 = 0;

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress2, boolean fromUser) {
                this.progress2 = progress2;
                //getUpdateTotal();
                textViewSeekBar2.setText(Integer.toString(progress2));
                Log.i("Seeker2: ", Integer.toString(progress2));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });


        buttonSum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSummaryTwoSeekBarValue(progress1, progress2);
                Log.i("", getSummaryTwoSeekBarValue(progress1, progress2));

            }
        });

    }

    public String getSummaryTwoSeekBarValue(int progress1, int progress2) {

        final TextView textViewMain_ = findViewById(R.id.textViewMain);
        int total = progress1 + progress2;
        String totalString = String.valueOf(total);
        Log.i("", totalString);
        textViewMain_.setText(String.valueOf(totalString));
        return totalString;
    }


}```
[enter image description here][1]


  [1]: https://i.stack.imgur.com/HDx12.png

您在主 class 和侦听器中都定义了 progress1 和 progress2,因此 class 变量将保持为 0,您必须使用 (your class name).this.progress1 = progress1; 并且在第二个搜索栏修改主 class 一个而不是听者一个


private int progress1;
private int progress2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView textViewMain;
        final TextView textViewMain_ = findViewById(R.id.textViewMain);
        SeekBar seekBar = findViewById(R.id.seekBar);
        final TextView textViewSeekBar = findViewById(R.id.textView2);
        SeekBar seekBar2 = findViewById(R.id.seekBar2);
        final TextView textViewSeekBar2 = findViewById(R.id.textView3);
        seekBar.setMax(600);
        seekBar.setProgress(5);
        seekBar2.setMax(900);
        seekBar2.setProgress(5);


        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress1, boolean fromUser) {
               getSummaryTwoSeekBarValue(progress1,progress2);
                textViewSeekBar.setText(Integer.toString(progress1));
                Log.i("Seeker1: ", Integer.toString(progress1));

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress2, boolean fromUser) {

          getSummaryTwoSeekBarValue(progress1,progress2);
                textViewSeekBar2.setText(Integer.toString(progress2));
                Log.i("Seeker2: ", Integer.toString(progress2));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

    }

    public String getSummaryTwoSeekBarValue(int progress1, int progress2) {//int progress1, int progress2
      this.progress1=progress1;
      this.progress2=progress2;

        final TextView textViewMain_ = findViewById(R.id.textViewMain);
        int total = progress1 + progress2;
        String totalString = String.valueOf(total);
        Log.i("", totalString);
        textViewMain_.setText(String.valueOf(totalString));
        return totalString;
    }
}```