如何修复Text Watcher问题?

时间:2015-11-25 18:23:04

标签: android android-edittext android-textwatcher

是的,我已就此主题提出了几个问题。它是关于textWatcher字段上的setText的。我对这一切都很陌生,也许这就是为什么Haven能够应用一个好的解决方案。我不是一个解释类型所以...... 这是我的代码:

package com.newera.speedsnfeeds;

import java.math.BigDecimal;
import java.math.RoundingMode;



import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
 import android.widget.EditText;

public class Main1Activity extends Activity implements TextWatcher {

private EditText dia, sfm, rpm,flutes,load,feed;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main1);

    dia = (EditText) findViewById(R.id.dia);
    dia.addTextChangedListener(this);

    sfm = (EditText) findViewById(R.id.sfm);
    sfm.addTextChangedListener(this);

    rpm = (EditText) findViewById(R.id.rpm);
    rpm.addTextChangedListener(this);

    flutes = (EditText) findViewById(R.id.flutes);
    flutes.addTextChangedListener(this);

    load = (EditText) findViewById(R.id.load);
    load.addTextChangedListener(this);

    feed = (EditText) findViewById(R.id.feed);
    feed.addTextChangedListener(this);




    rpm.setText("1");

    feed.setText("");
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
        int after) {
    // TODO Auto-generated method stub

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    // TODO Auto-generated method stub

}

@Override
public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub
    calculate();

}








@SuppressLint("UseValueOf")
private void calculate() {

    if (dia.getText().toString().compareTo("") == 0
            || sfm.getText().toString().compareTo("") == 0
            || rpm.getText().toString().compareTo("") == 0
            ||flutes.getText().toString().compareTo("") == 0
            ||load.getText().toString().compareTo("") == 0)

    {

        //rpm.setText("1");

        //feed.setText("1");




    }else{
        if (dia.getText().toString().compareTo(".") == 0
                || dia.getText().toString().compareTo("-") == 0
                || dia.getText().toString().compareTo("-0") == 0
                || dia.getText().toString().compareTo("-00") == 0
                || dia.getText().toString().compareTo("-000") == 0
                || dia.getText().toString().compareTo("-0000") == 0
                || dia.getText().toString().compareTo("-.") == 0


                || sfm.getText().toString().compareTo(".") == 0
                || sfm.getText().toString().compareTo("-") == 0
                || sfm.getText().toString().compareTo("-0") == 0
                || sfm.getText().toString().compareTo("-00") == 0
                || sfm.getText().toString().compareTo("-000") == 0
                || sfm.getText().toString().compareTo("-0000") == 0
                || sfm.getText().toString().compareTo("-.") == 0

                || rpm.getText().toString().compareTo("-") == 0
                || rpm.getText().toString().compareTo("-0") == 0
                || rpm.getText().toString().compareTo("-00") == 0
                || rpm.getText().toString().compareTo("-000") == 0
                || rpm.getText().toString().compareTo("-0000") == 0

                || flutes.getText().toString().compareTo("-") == 0
                || flutes.getText().toString().compareTo("-0") == 0
                || flutes.getText().toString().compareTo("-00") == 0
                || flutes.getText().toString().compareTo("-000") == 0
                || flutes.getText().toString().compareTo("-0000") == 0

                || load.getText().toString().compareTo("-") == 0
                || load.getText().toString().compareTo("-0") == 0
                || load.getText().toString().compareTo("-00") == 0
                || load.getText().toString().compareTo("-000") == 0
                || load.getText().toString().compareTo("-0000") == 0

                || feed.getText().toString().compareTo("-") == 0
                || feed.getText().toString().compareTo("-0") == 0
                || feed.getText().toString().compareTo("-00") == 0
                || feed.getText().toString().compareTo("-000") == 0
                || feed.getText().toString().compareTo("-0000") == 0)

        {


            //rpm.setText("1");

            //feed.setText("1");




        } else {
            float diam = Float.valueOf(dia.getText().toString());
            float sfmm = Float.valueOf(sfm.getText().toString());
            float rpmm = Float.valueOf(rpm.getText().toString());
            float flutesm = Float.valueOf(flutes.getText().toString());
            float loadm = Float.valueOf(load.getText().toString());

            BigDecimal trpm = new BigDecimal((sfmm *3.82 / diam));
            trpm = trpm.setScale(0,RoundingMode.UP);

            BigDecimal tfeed = new BigDecimal((rpmm * flutesm * loadm));
            tfeed = tfeed.setScale(1,RoundingMode.UP);




            rpm.setText("1"+ trpm);
            feed.setText("1"+tfeed);



        }


    }



    {



    }

   }


}

尚未完成我需要让它能够计算sfm和负载是它的原因 需要编辑文本字段。

我已经阅读过有关同一问题的其他问题,但他们与我无法应用这些想法的情况完全不同。可能因为我对编程很新。

希望这是一个开始提问的好方法。

P.S。抱歉这个杂乱的代码希望有代码可以帮助别人告诉我如何将修复程序应用到我当前的代码。如果太不清楚,也不要犹豫,让我知道,还需要xml等。

0 个答案:

没有答案