可以输入小数 & 错误处理

来源:互联网 发布:unity3d人物移动转向 编辑:程序博客网 时间:2024/04/30 15:27

将main.xml文件内容修改

 

<EditText android:id="@+id/weight"
 android:layout_width="fill_parent"
  android:layout_height="wrap_content"
 android:numeric="integer"
 android:text=""
 />

 

改为: android:singleLine="true"

public void onClick(View v)
    {
        DecimalFormat nf=new DecimalFormat("0.00");   
        try{
        double height=Double.parseDouble(field_height.getText().toString())/100;
        double weight=Double.parseDouble(field_weight.getText().toString());
       
       
        double bmi=weight/(height*height);       
       
        view_result.setText("Your bmi is "+nf.format(bmi));   
       
        if(bmi>25)
        view_suggest.setText(R.string.advice_heavy);
        else if(bmi<20)
            view_suggest.setText(R.string.advice_light);
        else view_suggest.setText(R.string.advice_average);   
        openOptionsDialog();
        }
        catch(Exception obj)
        {
            Toast.makeText(bmi.this, R.string.input_error, Toast.LENGTH_SHORT).show();
        }
    }

原创粉丝点击