单选按钮 - 小数位选项

时间:2018-05-14 08:19:16

标签: java android radio-button

我正在为你做一个为某些类型的数学做准备的程序。用户输入一个数字并点击一个按钮,将它们带到另一个活动,并显示答案加上公式等等。但有时候答案的小数点后面会有一个疯狂的数字。

现在,我已经知道如何更改小数点后的数字量,但是我希望使用editText在同一页面上使用单选按钮向用户提供他们想要多少位数的选项。

我该怎么做?

这是我到目前为止所拥有的......

输入页面w / radiobuttons -

public void click(查看视图){

    boolean checked = ((RadioButton) view).isChecked();

    switch (view.getId()){
        case R.id.radioButton1:
            if (checked){
                Intent check = new Intent(this, Output.class);
                String value = "0.0";
                check.putExtra("PATTERN", value);
            }
        break;
        case R.id.radioButton2:
            if (checked){
                Intent check = new Intent(this, Output.class);
                String value = "0.00";
                check.putExtra("PATTERN", value);
            }
            break;
        case R.id.radioButton3:
            if (checked){
                Intent check = new Intent(this, Output.class);
                String value = "0.000";
                check.putExtra("PATTERN", value);
            }
            break;
        case R.id.radioButton4:
            if (checked){
                Intent check = new Intent(this, Output.class);
                String value = "0.000000000";
                check.putExtra("PATTERN", value);
            }
            break;
    }

}

输出页面 -

Intent intent = getIntent();
String pattern = intent.getStringExtra("PATTERN");
String answerS = intent.getStringExtra("MESSAGE");

double fix = Double.parseDouble(answerS);
DecimalFormat dFormatter = new DecimalFormat(pattern);
String answerSS = "" + dFormatter.format(fix);

TextView answer = findViewById(R.id.answer);
answer.setText(answerSS);

this is the layout and what options i want

2 个答案:

答案 0 :(得分:0)

使用DecimalFormatter 取决于每个RadioButton:

double number = 3.14159265359;
DecimalFormat numberFormat = new DecimalFormat("#.0000");
System.out.println(numberFormat.format(number));

将输出: 3.1415

您可以使用或多或少的 0 扩展它,或者在分隔符之后使用#以及:

double number = 3.1500000000;
DecimalFormat numberFormat = new DecimalFormat("#.####");
System.out.println(numberFormat.format(number));

保留输出 3.15

答案 1 :(得分:0)

改变这件事......

switch (view.getId()){
    case R.id.radioButton1:
        if (checked){
            Intent check = new Intent(this, Output.class);
            String value = "0";
            check.putExtra("PATTERN", value);
        }
    break;
    case R.id.radioButton2:
        if (checked){
            Intent check = new Intent(this, Output.class);
            String value = "00";
            check.putExtra("PATTERN", value);
        }
        break;
    case R.id.radioButton3:
        if (checked){
            Intent check = new Intent(this, Output.class);
            String value = "000";
            check.putExtra("PATTERN", value);
        }
        break;
    case R.id.radioButton4:
        if (checked){
            Intent check = new Intent(this, Output.class);
            String value = "000000000";
            check.putExtra("PATTERN", value);
        }
        break;
}





    Intent intent = getIntent();
    String pattern = intent.getStringExtra("PATTERN");
    String answerS = intent.getStringExtra("MESSAGE");

    double fix = Double.parseDouble(answerS);
    DecimalFormat dFormatter = new DecimalFormat("#."+pattern);
    String answerSS = "" + dFormatter.format(fix);

    TextView answer = findViewById(R.id.answer);
    answer.setText(answerSS);