按钮单击不将结果返回到textview

时间:2012-07-22 05:24:15

标签: android android-button

我刚接触编码并且对我的知识有点不确定如果我忽略了一些简单的事情,请原谅我,但我一直在寻找2小时的答案。

我的一个课程将允许用户输入他们的身高和体重,并为他们返回BMI(身体质量指数)。问题是,一旦他们点击执行计算的按钮并在TextView中显示它,就没有任何反应。我从LogCat看到的唯一错误是getCursorCapsMode on inactive InputConnection。此错误仅在我使用next上的softkeyboard按钮在EditText字段之间移动时显示,因此我不相信这是相关的,因为当我在字段之间手动移动时仍然没有结果不要得到这个错误。如果您有任何信息,请随时协助解决此问题。

这是来自BMI android开发教程的重复代码,逐字逐句除了项目的id之外,但教程在加载到模拟器时工作得很好。我的代码如下:

public class FuncConverter extends DashMenuActivity {


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

    Spinner converter_spinner = (Spinner) findViewById(R.id.converter_spinner);
    ArrayAdapter<CharSequence> converter_adapter = ArrayAdapter.createFromResource(
            this, R.array.converter_array, android.R.layout.simple_spinner_item);

    converter_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        converter_spinner.setAdapter(converter_adapter);

}


public void calcClick (View view) {

    if (view.getId() == R.id.convert_btn_calc) {

        EditText weight_edit = (EditText) findViewById(R.id.weight_edit);
        EditText height_edit = (EditText) findViewById(R.id.height_edit);
        TextView bmi_text = (TextView) findViewById(R.id.bmi_text);

        float weight = Float.parseFloat(weight_edit.getText().toString());
        float height = Float.parseFloat(height_edit.getText().toString());

        float bmiValue = calculateBMI (weight, height);
        String bmiCalc = interpretBMI (bmiValue);

        bmi_text.setText(bmiValue + " - " + bmiCalc); 

    }

}


private float calculateBMI (float weight, float height) {
    return (float) (weight / (height * height));
}

private String interpretBMI(float bmiValue) {
    if (bmiValue < 16) {
        return "Severley Underweight";
    } else if (bmiValue < 18.5) {
        return "Underweight";
    } else if (bmiValue < 25) {
        return "Healthy weight";
    } else if (bmiValue < 30) {
        return "Overweight";
    } else {
        return "Obese";
    }
}}  

1 个答案:

答案 0 :(得分:0)

我很难肯定地回答,因为我不确定你是如何触发点击来计算东西的。但是从我所知道的calcClick(View)在你的xml中被定义为onClick=calcClick。如果这是真的那么你得到的信息是错误的。

public class FuncConverter extends DashMenuActivity {
EditText weight_edit;
EditText height_edit;
TextView bmi_text;

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

    Spinner converter_spinner = (Spinner) findViewById(R.id.converter_spinner);
    ArrayAdapter<CharSequence> converter_adapter = ArrayAdapter.createFromResource(
        this, R.array.converter_array, android.R.layout.simple_spinner_item);

    converter_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    converter_spinner.setAdapter(converter_adapter);

    //Moved these bad boys here for you instead
    weight_edit = (EditText) findViewById(R.id.weight_edit);
    height_edit = (EditText) findViewById(R.id.height_edit);
    bmi_text = (TextView) findViewById(R.id.bmi_text);

}


public void calcClick (View view) {

if (view.getId() == R.id.convert_btn_calc) { //dont't think you need this check to be honest



    float weight = Float.parseFloat(weight_edit.getText().toString());
    float height = Float.parseFloat(height_edit.getText().toString());

    float bmiValue = calculateBMI (weight, height);
    String bmiCalc = interpretBMI (bmiValue);

    bmi_text.setText(bmiValue + " - " + bmiCalc); 

    }

}

我在那里做的只是将EditTextTextView移动为全局变量,然后您可以在calcClick(View)中访问它。如果我是对的,那么它无效的原因是因为您无法访问EditText之外的xml中的TextViewonCreate()视图,至少在您的情况下如此。传递的View参数实际上是按钮或触发事件的任何内容。希望这是有道理的。因此,View view不是文档根,而是(Button)view或再次,无论您点击什么来触发它。