【问题标题】:Why cant i get these calculations to display on a textview?为什么我不能让这些计算显示在文本视图上?
【发布时间】:2012-01-05 09:01:38
【问题描述】:

我正在尝试制作一个使用微调器作为单位选择方法来转换距离/面积/体积的应用程序。计算意味着完成,然后根据输入到 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.
        }
    });

【问题讨论】:

    标签: java android textview android-edittext spinner


    【解决方案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);
    }
    

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

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

    【讨论】:

    • 非常感谢这对您有很大帮助。好吧,这不是我的大学工作,但我才刚刚开始在大学学习 Java,自从获得 Nexus One 后,我就一直想为 android 开发,所以我认为我可以尝试一些比网络上提供的大多数教程更复杂的东西。再次感谢
    • 没问题...ActionBar 也没有做任何有用的事情...如果不是 Uni/College 作业,您可能会发现 RoboGuice 对您的 android 应用程序很有用...如果您只是在学习Java 可能有点过头了。同样为 Nexus One 开发意味着您可能无法使用 ICS。在我看来,为早期版本的 Android 开发是令人沮丧的!祝你好运!
    • 我在我的 nexus 上安装了 ICS,确切地说是 XDA Developers 的开发人员提供的 4.0.3。所以我想为什么不先为 ICS 开发,然后让它在旧版本的 android 上工作。再次感谢
    【解决方案2】:

    从您发布的代码中很难分辨,但我猜您在选择选项 1 时设置了 doubleInput。如果您在选择选项 1 后输入数字,则它将始终为 0.0,因为更改微调器时该数字不在文本区域中。

    使用调试器单步调试代码,看看你是否到达了这条线

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

    并检查此时数字是否设置正确。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-26
      • 2014-05-19
      • 1970-01-01
      • 2018-01-03
      • 1970-01-01
      • 1970-01-01
      • 2020-11-28
      • 2016-04-18
      相关资源
      最近更新 更多