如果可能的话,我需要制作一个简单的TextWatcher计算器

时间:2017-08-20 09:42:43

标签: android calculator textwatcher

我想创建一个只包含3个EditText的应用。 (没有别的)

EditText1

EditText2

EditText3

现在

1)如果我输入EditText1和EditText2的值,则应在ET1和ET2之间执行添加,然后应将answer设置为EditText3 .... (ET1 + ET2 = ET3)

2)如果我输入EditText1和EditText3的值,则应在ET1和ET3之间执行减法,然后应将answer设置为EditText2 .... (ET3-ET1 = ET2)

1 个答案:

答案 0 :(得分:0)

这听起来很疯狂,但我认为,“以前没做过!并不意味着永远无法完成。”

@confused soul,我为你的疯狂问题找到了解决方案......

EditText

的Xml
 <EditText
      android:id="@+id/et_a"
      android:layout_width="0dp"
      android:layout_height="40dp"
      android:layout_weight="0.16"
      android:gravity="center"
      android:inputType="number" />

 <EditText
      android:id="@+id/et_b"
      android:layout_width="0dp"
      android:layout_height="40dp"
      android:layout_weight="0.16"
      android:gravity="center"
      android:inputType="number" />

 <EditText
      android:id="@+id/et_c"
      android:layout_width="0dp"
      android:layout_height="40dp"
      android:layout_weight="0.16"
      android:gravity="center"
      android:inputType="number"/>
 </LinearLayout>

我为解决方案所做的代码......

public class MainActivity extends AppCompatActivity {

EditText et_a, et_b,et_c;

String whereChanging = "a";
String whereFocusing = "a";
String whereLoseFocusing = "b";

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

    et_a = (EditText) findViewById(R.id.et_a);
    et_b = (EditText) findViewById(R.id.et_b);
    et_c = (EditText) findViewById(R.id.et_c);

    et_a.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus) {
                whereFocusing = "a";
                Log.e("Focus", "A is Focusing....");
            }else{
                whereLoseFocusing = "a";
                Log.e("LoseFocus", "A is Lose it's Focus....");
            }
        }
    });

    et_b.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus) {
                whereFocusing = "b";
                Log.e("Focus", "B is Focusing....");
            }else{
                whereLoseFocusing = "b";
                Log.e("LoseFocus", "B is Lose it's Focus....");
            }
        }
    });

    et_c.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus) {
                whereFocusing = "c";
                Log.e("Focus", "C is Focusing....");
            }else{
                whereLoseFocusing = "c";
                Log.e("LoseFocus", "C is Lose it's Focus....");
            }
        }
    });


    et_a.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        public void beforeTextChanged(CharSequence s, int start, int count,int after) {
            if(whereFocusing.equals("a")){
                whereChanging = "a";
            }
        }

        public void afterTextChanged(Editable s) {

            if(whereFocusing.equals("a") && whereLoseFocusing.equals("b")) {
                try {
                    if (whereChanging.equals("a")) {
                        Log.e("Typing", "A is typing....");
                        if (s.length() > 0) {
                            int a_input = Integer.parseInt(s.toString());
                            String b_text = et_b.getText().toString();
                            int b_input = 0;
                            if (!b_text.isEmpty()) {
                                b_input = Integer.parseInt(b_text);
                            }
                            et_c.setText((a_input + b_input) + "");
                        }
                    }
                } catch (Exception exception) {
                    Log.e("Err", exception.toString() + "");
                }
            }else if(whereFocusing.equals("a") && whereLoseFocusing.equals("c")) {
                try {
                    if (whereChanging.equals("a")) {
                        Log.e("Typing", "A is typing....");
                        if (s.length() > 0) {
                            int a_input = Integer.parseInt(s.toString());
                            String c_text = et_c.getText().toString();
                            int c_input = 0;
                            if (!c_text.isEmpty()) {
                                c_input = Integer.parseInt(c_text);
                            }
                            et_b.setText((c_input-a_input)+"");
                        }
                    }
                } catch (Exception exception) {
                    Log.e("Err", exception.toString() + "");
                }
            }
        }
    });
    et_b.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        public void beforeTextChanged(CharSequence s, int start, int count,int after) {
            if(whereFocusing.equals("b")){
                whereChanging = "b";
            }
        }

        public void afterTextChanged(Editable s) {


            try{
                if(whereChanging.equals("b")) {
                    Log.e("Typing", "B is typing....");
                    if(s.length()>0) {
                        int b_input = Integer.parseInt(s.toString());
                        String a_text = et_a.getText().toString();
                        int a_input = 0;
                        if (!a_text.isEmpty()) {
                            a_input = Integer.parseInt(a_text);
                        }
                        et_c.setText((a_input + b_input)+"");
                    }
                }
            }catch(Exception exception){
                Log.e("Err", exception.toString()+"");
            }

        }
    });
    et_c.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        public void beforeTextChanged(CharSequence s, int start, int count,int after) {
            if(whereFocusing.equals("c")){
                whereChanging = "c";
            }
        }

        public void afterTextChanged(Editable s) {

            try{
                if(whereChanging.equals("c")) {
                    Log.e("Typing", "C is typing....");
                    if(s.length()>0) {
                        int c_input = Integer.parseInt(s.toString());
                        String a_text = et_a.getText().toString();
                        int a_input = 0;
                        if (!a_text.isEmpty()) {
                            a_input = Integer.parseInt(a_text);
                        }
                        et_b.setText((c_input-a_input)+"");
                    }
                }
            }catch(Exception exception){
                Log.e("Err", exception.toString()+"");
            }

        }
    });
 }
}

我尝试了你的疯狂想法,它的工作文件.....我希望它会帮助你... 谢谢你的想法...我喜欢它:))