如何使SeekBar值上升5s?以及如何将用户选择的搜索栏值传递给方法?

时间:2016-02-19 00:01:09

标签: java android seekbar

我有两个问题,首先,我希望我的搜索栏显示如下值:5,10,15,20 ...... 30。我设置了max = 30和progress = 5,但事实并非如此。其次,在用户单击Calculate按钮后,我需要用户通过搜索栏选择的值传递给calculatePayments()方法。有人请帮帮我吗?我会很感激!谢谢!

公共类AKMainActivity扩展了Activity {

private EditText loanAmount, interestRate, loanYears; 
private TextView monthlyPaymentsResult, totalPaymentsResult, seek_barTV; 
private static SeekBar seek_bar;
private static TextView textview_seek;


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

    seekbarr();
    loanAmount = (EditText)findViewById(R.id.loanET);
    interestRate = (EditText) findViewById(R.id.intrateET);
    monthlyPaymentsResult = (TextView) findViewById(R.id.monthlyResultTV);
    totalPaymentsResult = (TextView) findViewById(R.id.totalResultsTV);

}

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) {
            // TODO Auto-generated method stub

        }

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

            years_number = progress;
            textview_seek.setText(years_number + " Years");
            progress = seek_bar.getProgress();


        }
    });

}


public void calculatePayments(View clickedButton) {

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

    double years = seek_bar.getProgress();

    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));
}

}

1 个答案:

答案 0 :(得分:0)

要设置进度增量,您可以执行以下操作:

seekBar.incrementProgressBy(5);

您还需要获得对按钮的引用,就像您为其他视图(TextView,SeekBar等)所做的那样,并且在Button的onClick侦听器中,您可以调用calculatePayments方法,并将当前的搜索条值设置为一个论点。要获取搜索栏的当前值,请使用:

seekBar.getProgress();