Android 动态实现EditText字段时出现问题

Android 动态实现EditText字段时出现问题,android,android-edittext,Android,Android Edittext,我正在我的EditText字段中输入浮点数,希望根据如下逻辑计算结果,并在TextView中显示结果。我想知道这个方法的问题是什么,因为我在这个特定的方法中遇到了错误。我知道这是一件简单的事,但我是一个Android初学者 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layo

我正在我的EditText字段中输入浮点数,希望根据如下逻辑计算结果,并在TextView中显示结果。我想知道这个方法的问题是什么,因为我在这个特定的方法中遇到了错误。我知道这是一件简单的事,但我是一个Android初学者

    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.bmi);
            bmi1=(EditText) findViewById(R.id.editText1);
            bmi2=(EditText) findViewById(R.id.editText2);
          bmi3=(TextView) findViewById(R.id.bmiText);


    bmi2.addTextChangedListener(new TextWatcher(){
        @Override
        public void afterTextChanged(Editable ab) {
            // TODO Auto-generated method stub

        }
          public void beforeTextChanged(CharSequence s, int start, int count, int after){}
        public void onTextChanged(CharSequence s, int start, int before, int count){
            calRes();
        }
    });




    ImageButton ImBtn=(ImageButton)findViewById(R.id.imageButton1);
    ImBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent newIntent=new Intent(Activity1.this,DragNDropListActivity.class);
            startActivity(newIntent);
            finish();
        }
    });

}

    public void calRes() {
        String a; 
         String b;  
        float c;
         a =  bmi1.getEditableText().toString();   //gets you the contents of edit text
         b =  bmi2.getEditableText().toString(); 
         c=  Float.parseFloat(a)/(Float.parseFloat(b)*Float.parseFloat(b));    
         bmi3.setText(String.valueOf(c));
    }

}
错误: 而不是这个

bmi3.setText(String.valueOf(c));
这样做

bmi3.setText(""+c);
编辑:
使用
valueOf
而不是
parseFloat

  c=  Float.valueOf(a)/(Float.valueOf(b)*Float.valueOf(b)); 
编辑2: 当所有值都输入到
编辑文本中时,计算BMI。有一个按钮并在按钮的
onClick
中调用
calRes
。您将获得异常,因为当您仍在输入第一个字段且其余字段为空时,会调用calRes。空字段导致崩溃,因为它们中没有要转换的值。

可能不是这样

    a =  bmi1.getText().toString();   //gets you the contents of EditText
    b =  bmi2.getText().toString(); 
你应该试试这个

    a =  bmi1.getEditableText().toString();   //gets you the contents of EditText
    b =  bmi2.getEditableText().toString(); 

如果您的
EditText
(bmi1和bmi2,a,b)中有任何字母或特殊字符,则此错误一定会出现,因此请使用
System.out.println
检查您的EditText值。因为浮点变量必须是数字和点(.)

好的,在查看代码后,您在解析要浮点的字符串时遇到问题,

如果字符串a和b的值没有问题,那么它应该可以正常工作

听着,我已经试过了,效果很好

       String a,b;
       Float c;
      tv =(TextView)findViewById(R.id.tv_test);
        a =  "100";   //gets you the contents of EditText
        b =  "10"; 
        c=  Float.parseFloat(a)/(Float.parseFloat(b)*Float.parseFloat(b));    
      tv.setText(String.valueOf(c));


Edit: Also take care of your editText input its have only numeric values in string
format, not any characters or special symbols.

从您的初始错误来看,似乎您正试图解析字符串“lengte in meter”中的浮点。这是布局中标签的文本吗?它确实不是可解析的数字:) 如果您仍然有问题,可以发布xml布局文件


Anthony

你能提供错误日志跟踪吗?@user370305:我已经添加了错误日志。@Chirag Raval::我已经在onclick上添加了错误日志调用
calRes
。查看我编辑的答案。user7777:即使这样,我也看不到任何变化。@user77float.parseFloat()它也工作正常。我认为字符串值有问题。我在一个EditText字段中输入100,每当我想在另一个EditText字段中输入值时,它都会给我错误。你在错误的位置计算值。在edittext失去焦点后调用calRes。当所有值都输入edittext时计算BMI:即使将其设置为EditableText(),我也看不到任何更改。我已对整个类编辑了代码。请看一看。像这样尝试,bmi3.setText(Float.toString(c));我得到了解决方案。无论如何,谢谢:)我有一个小疑问。如何在我的EditText字段中动态地将小数设置为两个值(即#.#.##)?向EditText添加一个textChangedListener,并在其中检查已添加的小数位数。如果他们有2个alread,不要再接受任何输入。我不知道一个简单的掩码方法,你可以使用。
       String a,b;
       Float c;
      tv =(TextView)findViewById(R.id.tv_test);
        a =  "100";   //gets you the contents of EditText
        b =  "10"; 
        c=  Float.parseFloat(a)/(Float.parseFloat(b)*Float.parseFloat(b));    
      tv.setText(String.valueOf(c));


Edit: Also take care of your editText input its have only numeric values in string
format, not any characters or special symbols.