多个editText,textchangedlistener仅在填充了所有editText时才有效

时间:2013-04-26 12:38:28

标签: android android-edittext textwatcher

我尝试做一个小的界面,其中我放了一个数字,计算的结果显示在文本字段中。我在同一个界面上进行了几次操作。所以我尝试了一个常规的addtextchangedlistener,它可以工作,但我注意到我需要填充每个editText,我在其上添加了一个监听器,以便它工作。

这是我的第一个代码:

package be.thebeps.easyconverter;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.*;
import android.text.*;

public class MainActivity extends Activity implements TextWatcher  {

    private Double calcula;
    private EditText calcula1;
    private TextView calcula2;
    private Double calculb;
    private EditText calculb1;
    private TextView calculb2;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        calcula1 = (EditText) findViewById(R.id.calcula1);
        calcula1.addTextChangedListener(this);
        calcula2 = (TextView) findViewById(R.id.calcula2);
        calculb1 = (EditText) findViewById(R.id.calculb1);
        calculb1.addTextChangedListener(this));
        calculb2 = (TextView) findViewById(R.id.calculb2);



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }





    @Override
    public void afterTextChanged(Editable s) {

        try {

            calcula = Double.parseDouble(calcula1.getText().toString());
            calcula = calcula*0.621;
            calcula2.setText(String.format("%.3f", calcula));
            calculb = Double.parseDouble(calculb1.getText().toString());
            calculb = calculb*2.204;
            calculb2.setText(String.format("%.2f", calculb));



        } catch(NumberFormatException nfe) {
            calcula2.setText(Integer.toString(0));
            calculb2.setText(Integer.toString(0));


        }
    }   


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


    }

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


    }   

}

package be.thebeps.easyconverter; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.*; import android.text.*; public class MainActivity extends Activity implements TextWatcher { private Double calcula; private EditText calcula1; private TextView calcula2; private Double calculb; private EditText calculb1; private TextView calculb2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); calcula1 = (EditText) findViewById(R.id.calcula1); calcula1.addTextChangedListener(this); calcula2 = (TextView) findViewById(R.id.calcula2); calculb1 = (EditText) findViewById(R.id.calculb1); calculb1.addTextChangedListener(this)); calculb2 = (TextView) findViewById(R.id.calculb2); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override public void afterTextChanged(Editable s) { try { calcula = Double.parseDouble(calcula1.getText().toString()); calcula = calcula*0.621; calcula2.setText(String.format("%.3f", calcula)); calculb = Double.parseDouble(calculb1.getText().toString()); calculb = calculb*2.204; calculb2.setText(String.format("%.2f", calculb)); } catch(NumberFormatException nfe) { calcula2.setText(Integer.toString(0)); calculb2.setText(Integer.toString(0)); } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int count, int after) { } }

然后因为它没有按预期工作,我试图在每个editText字段上实现一个用于instanciate textWatcher的类,所以这是我的实际代码:

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.*;
import android.text.*;

public class MainActivity extends Activity {

    private Double calcula;
    private EditText calcula1;
    private TextView calcula2;
    private Double calculb;
    private EditText calculb1;
    private TextView calculb2;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        calcula1 = (EditText) findViewById(R.id.calcula1);
        calcula1.addTextChangedListener(new CustomTextWatcher(calcula1));
        calcula2 = (TextView) findViewById(R.id.calcula2);
        calculb1 = (EditText) findViewById(R.id.calculb1);
        calculb1.addTextChangedListener(new CustomTextWatcher(calculb1));
        calculb2 = (TextView) findViewById(R.id.calculb2);



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }




    private class CustomTextWatcher implements TextWatcher {
        private EditText mEditText;

        public CustomTextWatcher(EditText e) { 
            mEditText = e;
        }

        @Override
        public void afterTextChanged(Editable s) {

            try {

                calcula = Double.parseDouble(calcula1.getText().toString());
                calcula = calcula*1.451;
                calcula2.setText(String.format("%.3f", calcula));
                calculb = Double.parseDouble(calculb1.getText().toString());
                calculb = calculb*3.206;
                calculb2.setText(String.format("%.2f", calculb));



            } catch(NumberFormatException nfe) {
                calcula2.setText(Integer.toString(0));
                calculb2.setText(Integer.toString(0));

            }
        }   


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


    }

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


    }   

    }
}

但是,如果只填充了2个字段中的一个,我更改它时它不会更新(我的意思是没有调用aftertextchanged方法)。我想在创建视图时已经填充了editText但是,如果有人进入一个editText(例如calcula1)并清除它(例如带退格)然后进入calculb1,他可以输入他想要的每个数字, calculb2不会被修改。我知道这很棘手,我有点挑剔,但如果有人可以帮助我,我会很高兴。

1 个答案:

答案 0 :(得分:0)

将此代码放在您的第一个代码

 @Override
    public void afterTextChanged(Editable s) {
        try {
            calcula = Double.parseDouble(calcula1.getText().toString());
            calcula = calcula*0.621;
            calcula2.setText(String.format("%.3f", calcula));
        } catch(NumberFormatException nfe) {
            Log.e("MainActivity", "NumberFormatException at parseDouble for calculb", nfe);
            calcula2.setText(Integer.toString(0));
        }
        try {
            calculb = Double.parseDouble(calculb1.getText().toString());
            calculb = calculb*2.204;
            calculb2.setText(String.format("%.2f", calculb));
        } catch(NumberFormatException nfe) {
            Log.e("MainActivity", "NumberFormatException at parseDouble for calculb", nfe);
            calculb2.setText(Integer.toString(0));
        }
    }

原因是你在第一个Double.parseDouble()获得NFE。

calcula1.getText()。toString()将返回NULL,解析NULL将为五个NFE。