Float之间的减法

时间:2015-03-14 08:25:51

标签: android math methods stack-overflow

请大家帮帮我!我疯了!下面是我的代码的简短摘要,应该用于进行简单的减法。我应该只读SCONTRINO的数量,如果你把CONTANTI,VINCITE字段,将具有setText SCONTRINO - CONTANTI,与VINCITE一样,将CONTANTI.setText SCONTRINO - VINCITE。 但是尽管一切似乎写得很好,当我插入一个字段时,我通过两个Update方法得到StackOverflowError。

public class AssegnaScontoActivity extends Activity {
    TextView contanti;
    TextView vincite;
    TextView scontrino;
    Float contantiFloat;
    Float vinciteFloat;
    Float scontrinoFloat;


  public void onCreate(Bundle savedInstanceState) {
    //INIZIALIZZAZIONE ACTIVITTY
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.assegna_sconto_activity_landscape);
    //--------------------------  

    contantiFloat = Float.parseFloat(contanti.getText().toString());
    vinciteFloat = Float.parseFloat(vincite.getText().toString());
    scontrinoFloat = Float.parseFloat(1000);
    contanti = (TextView) findViewById(R.id.contanti);
    vincite = (TextView) findViewById(R.id.importo_vincite);
    scontrino = (TextView) findViewById(R.id.importo_scontrino);        


  contanti.addTextChangedListener(new TextChangedListener()
    {
        @Override
        public void numberEntered(Float number)
        {
            contantiFloat = number;
            updateVincite();

        }
    });
    vincite.addTextChangedListener(new TextChangedListener()
    {

        @Override
        public void numberEntered(Float number)
        {
            vinciteFloat = number;
            updateContanti();
        }
    });
    }

private void updateVincite()
{
    Float total = scontrinoFloat - contantiFloat; // This is where you apply your function
    vincite.setText(""+total); // need to do that otherwise int will
    // be treated as res id.
}

private void updateContanti()
{

    Float total = scontrinoFloat - vinciteFloat; // This is where you apply your function
    contanti.setText(""+total); // need to do that otherwise int will
    // be treated as res id.
}

private abstract class TextChangedListener implements TextWatcher
{

    public abstract void numberEntered(Float number);

    @Override
    public void afterTextChanged(Editable s)
    {
        String text = s.toString();
        try
        {
            Float parsedFloat = Float.parseFloat(text);
            numberEntered(parsedFloat);
        } catch (NumberFormatException e)
        {
            Log.w(getPackageName(), "Non si puo' parsare '" + text + "' col numero", e);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after)
    {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count)
    {
    }
}

2 个答案:

答案 0 :(得分:0)

您的代码进入无限循环,因为您在调用afterTextChanged()时更改了文本,这会导致再次调用afterTextChanged()等等,直到最终溢出调用堆栈。

如果与当前文本不同,可以通过仅设置updateVincite()和updateContanti()内的文本来停止此操作。

e.g:

private void updateVincite()
{
    Float total = scontrinoFloat - contantiFloat; // This is where you apply your function
    String text = ""+total;
    if(!vincite.getText().toString().contentEquals(text))
        vincite.setText(text); // need to do that otherwise int will
    // be treated as res id.
}

并对updateContanti()

执行相同操作

答案 1 :(得分:0)

首先定义contantivincite

更改代码如下。

contanti = (TextView) findViewById(R.id.contanti);
vincite = (TextView) findViewById(R.id.importo_vincite);
scontrino = (TextView) findViewById(R.id.importo_scontrino);     
contantiFloat = Float.parseFloat(contanti.getText().toString());
vinciteFloat = Float.parseFloat(vincite.getText().toString());
scontrinoFloat = Float.parseFloat(1000);