如何将SeekBar值应用于方法中的计算?

时间:2016-02-17 05:56:48

标签: java android eclipse android-studio seekbar

我希望能够获取用户选择的搜索栏值(年数)并将其添加到calculatePayments(View clickedButton)方法。然后将此搜索条值除以1200,如下面的代码所示,但我很困惑如何将搜索条值与数学计算联系起来。有帮助吗?谢谢!

public class MainActivity extends Activity {
    private EditText loanAmount, interestRate, loanYears;
    private TextView monthlyPaymentsResult, totalPaymentsResult;
    private static SeekBar seek_bar;
    private static TextView textview_seek;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        seekbarr();
        loanAmount = (EditText) findViewById(R.id.loanAmtET);
        interestRate = (EditText) findViewById(R.id.intRateET);
        //loanYears = (EditText) findViewById(R.id.);
        monthlyPaymentsResult = (TextView) findViewById(R.id.monthlyResultTV);
        totalPaymentsResult = (TextView) findViewById(R.id.totalResultTV);
    }

    public void seekbarr() {
        seek_bar = (SeekBar) findViewById(R.id.seekBar1);
        textview_seek = (TextView) findViewById(R.id.seekbarTV);
        textview_seek.setText(seek_bar.getProgress() + "Years");

        seek_bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            int years_number = 5;

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                textview_seek.setText(years_number + " Years");
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                years_number = progress;
                textview_seek.setText(years_number + " Years");
            }
        });
    }

    public void calculatePayments(View clickedButton) {
        double loan = Integer.parseInt(loanAmount.getText().toString());
        double interest = (Integer.parseInt(interestRate.getText().toString()));

        double years = Integer.parseInt(textview_seek.getText().toString());

        double r = years / 1200;
        double r1 = Math.pow(r + 1, years);

        double monthlyPayment = (double) ((r + (r / (r1 - 1))) * loan);
        double totalPayment = monthlyPayment * years;

        monthlyPaymentsResult.setText(new DecimalFormat("##.##").format(monthlyPayment));
        totalPaymentsResult.setText(new DecimalFormat("##.##").format(totalPayment));
    }
}

2 个答案:

答案 0 :(得分:0)

SeekBar类是ProgressBar的子类,它包含一个getProgress()方法。

调用seek_bar.getProgress()将返回您要查找的值。

答案 1 :(得分:0)

编辑您的方法

public void calculatePayments(int year) {

    double loan = Integer.parseInt(loanAmount.getText().toString());
    double interest = (Integer.parseInt(interestRate.getText().toString()));

    int years = year;

    double r = years / 1200;
    double r1 = Math.pow(r + 1, years);

    double monthlyPayment = (double) ((r + (r / (r1 - 1))) * loan);
    double totalPayment = monthlyPayment * years;

    monthlyPaymentsResult.setText(new DecimalFormat("##.##").format(monthlyPayment));
    totalPaymentsResult.setText(new DecimalFormat("##.##").format(totalPayment));
}

现在调用此方法。

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

        calculatePayments(profress);
        textview_seek.setText(years_number + " Years");

    }