Android:如何将单选按钮的文本设置为整数?

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

Android: How to set text of radio button into an Integer?

问题

我需要从 RadioGroup 中检索值并根据贷款期限将 Radio 按钮上的文本设置为整数

**PurchaseActivity.java**

    Auto mAuto;

    // 要传递到贷款活动的数据
    String loanReport;
    String monthlyPayment;

    // 布局输入引用
    EditText carPriceET;
    EditText downPayET;
    RadioGroup loanTermRG;

    private void collectAutoInputData() {
        // 任务 1: 设置汽车价格
        mAuto.setPrice((double) Integer.valueOf(carPriceET.getText()
                .toString()));

        // 任务 2: 设置首付款
        mAuto.setDownPayment((double)
                Integer.valueOf(downPayET.getText()
                        .toString()));

        // 任务 3: 设置贷款期限
        Integer radioId = loanTermRG.getCheckedRadioButtonId();
        RadioButton term = (RadioButton) findViewById(radioId);
        mAuto.setLoanTerm(term.getText().toString());
    }
英文:

I am needing to retrieve the values from a RadioGroup and set the Text on the Radio Buttons to an Integer based on the loan term.

PurchaseActivity.java

 Auto mAuto;

// THE DATA TO BE PASSED TO THE LOAN ACTIVITY
String loanReport;
String monthlyPayment;

// LAYOUT INPUT REFERENCES
EditText carPriceET;
EditText downPayET;
RadioGroup loanTermRG;

private void collectAutoInputData() {
    // TASK 1: SET THE CAR PRICE
    mAuto.setPrice ((double) Integer.valueOf(carPriceET.getText()
            .toString()));

    //TASK 2: SET THE DOWN PAYMENT
    mAuto.setDownPayment((double)
            Integer.valueOf(downPayET.getText()
                    .toString()));

    //TASK 3 SET THE LOAN TERM
    Integer radioId = loanTermRG.getCheckedRadioButtonId();
    RadioButton term = (RadioButton) findViewById(radioId);
    mAuto.setLoanTerm(term.getText().toString());
}

答案1

得分: 2

使用String.valueOf();

  mAuto.setLoanTerm(String.valueOf(term.getText()));

英文:

Use String.valueOf();

  mAuto.setLoanTerm(String.valueOf(term.getText()));

huangapple
  • 本文由 发表于 2020年10月5日 01:24:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64197695.html
匿名

发表评论

匿名网友

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

确定