为什么我不能将这些计算显示在textview上?

时间:2012-01-04 17:32:03

标签: java android textview android-edittext spinner

我试图制作一个使用微调器作为单位选择方法转换距离/面积/体积的应用。计算完成,然后根据输入EditText的内容将输出发送到textview。但输出只有0.0而且没有别的。任何人都可以帮忙吗?

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            switch(pos){
            case 0:         
                option1.setAdapter(length);
                option2.setAdapter(length);
                return;
            case 1:         
                option1.setAdapter(area);
                option2.setAdapter(area);
                return;
            default:
        }           
    }

        public void onNothingSelected(AdapterView<?> parent) {
          // Do nothing.
        }
    });

    option1.setOnItemSelectedListener(new OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            if(pos>=0) {
                stringInput = edittext1.getText().toString();
                if(stringInput == null || stringInput.isEmpty()) {
                    doubleInput = 0.0;
                }
                else {
                    doubleInput = Double.parseDouble(edittext1.getText().toString());
                }                                   
            }                               
        }

        public void onNothingSelected(AdapterView<?> parent) {
          // Do nothing.
        }
    });     

    option2.setOnItemSelectedListener(new OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            switch(pos) {
            case 0:
                miles = doubleInput * 1;
                textview1.setText("" + String.valueOf(miles));
                return;
            case 1:
                km = doubleInput * 1.609344;
                textview1.setText("" + String.valueOf(km));
                return;
            default:
            }

        }

        public void onNothingSelected(AdapterView<?> parent) {
          // Do nothing.
        }
    });

2 个答案:

答案 0 :(得分:1)

听起来像一个真正的老人,这看起来像你的大学作业......是吗?

自提问以来,您的代码已经发生了变化,这使得回答非常困难!幸运的是,在您更改之前我设法将您的代码扫描到Eclipse中......无论如何,在您的原始代码中,您在create方法中执行所有操作,此时您还没有输入edittext1的值(除非它设置为某些合理的默认值,我认为它是0,因此总是得到零作为你的答案?)

    // Whilst setting up a view the create method will not have a 
    // reasonable value for edittext1 - or it will be your default
    String stringInput = (edittext1.getText().toString());
if (stringInput.isEmpty()) { 
    doubleInput = 0.0;    // Will always enter this line
} else {
    doubleInput = Double.parseDouble(edittext1.getText().toString());
}

你复制了代码......

output1 = (TextView) findViewById(R.id.output1);

实际上,output1到output10(即所有十行)都是重复的。

至于您的更新代码,它仍然会给您一个问题吗?你确定stringInput有一个值吗?我的意思是你输入了什么?您可以通过调试程序进行检查..

以下内容也很容易出错,因为FloatingCoder建议,并且可能会破坏...

doubleInput = Double.parseDouble(edittext1.getText().toString());

更好的方法(因为它捕获了Java可能抛出的异常)是

doubleInput = 0.0;
String inputStr = question.getText().toString();
try {
    doubleInput = Double.parseDouble(inputStr);
} catch (NumberFormatException e) {
    // This should probably do something more useful? i.e. tell
    // the user what they've done wrong...
    Log.e("Android",
                    "Double throws a NumberFormatException if you enter text that is not a number");
}

哦,Android有一些帮助实用程序来检查字符串,请参阅TextUtils,或者只是我的示例......

if (!TextUtils.isEmpty(inputStr)) { // checks for "" and null (see documentation)
      doubleInput = Double.parseDouble(inputStr);
}

我真的建议为一个看起来不正确的计算编写一个简单的测试用例,因为两个文本框和一个按钮确实不易拼凑而且非常容易调试而不需要所有的微调器进入方式...无论如何希望这有帮助,哦,我的完整示例有两个edittexts和一个按钮,我只是在这里发布...希望它有帮助......

private Button btnCalc;
private EditText question, answer;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    answer = (EditText) findViewById(R.id.answer);
    question = (EditText) findViewById(R.id.question);
    btnCalc = (Button) findViewById(R.id.btnCalc);
            // The OnClickListener here will be executed outside the "Create",
            // i.e., when you actually click on the button, which will give you
            // a chance to enter some values in the question edittext...
    btnCalc.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            double in = 0.0;
            try {
                String inputStr = question.getText().toString();

                // if you want to check it use
                if (!TextUtils.isEmpty(inputStr)) {
                    in = Double.parseDouble(inputStr);
                }
            } catch (NumberFormatException e) {
                // This should probably do something more useful? i.e. tell
                // the user what they've done wrong...
                Log.e("Android",
                        "Double throws a NumberFormatException if you enter text that is not a number");
            }

            double miles = in * 1.6;
            answer.setText(String.valueOf(miles));
        }
    });
}

答案 1 :(得分:0)

从你发布的代码中说出来有点难,但我的猜测是你在选择选项1时设置了doubleInput。如果在选择选项1后输入数字,它将始终为0.0,因为更改微调器时数字不在文本区域中。

使用调试器逐步执行代码,看看您是否到达了该行

doubleInput = Double.parseDouble(edittext1.getText().toString());

并检查该点是否正确设置了数字。