Android Java两个SeekBar的值求和,使用setOnSeekBarChangeListener或按钮

huangapple go评论75阅读模式
英文:

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

问题


    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;
                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;
                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;
    }
}
英文:

being new in Android I'm trying to just simply add (sum) adjusted values from two SeekBars using setOnSeekBarChangeListener or using Button.
The project below is work fine but the sum still is 0. I can't find a solution. Thanks for your help. Usually, I'm using Spring Boot and Vaadin and it is so simple Android Java两个SeekBar的值求和,使用setOnSeekBarChangeListener或按钮


    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

</details>


# 答案1
**得分**: 0

你正在主类和监听器中定义progress1和progress2,因此类变量将继续保持为0,你必须使用 `(你的类名).this.progress1 = progress1;`,在第二个seek bar中也是同样的操作,修改主类中的变量而不是监听器中的变量。

<details>
<summary>英文:</summary>

You are defining progress1 and progress2 in both the main class and in the listener, so the class variable will keep being 0, you have to use `(your class name).this.progress1 = progress1;` and same in the second seek bar to modify the main class one instead of the listener one

</details>



# 答案2
**得分**: 0

```java
public class MainActivity extends AppCompatActivity {

    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) {
        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;
    }
}
英文:

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(&quot;Seeker1: &quot;, 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(&quot;Seeker2: &quot;, 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(&quot;&quot;, totalString);
        textViewMain_.setText(String.valueOf(totalString));
        return totalString;
    }
}```

</details>



huangapple
  • 本文由 发表于 2020年9月20日 04:27:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63973135.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定