Android在textedit上添加观察者

时间:2014-12-10 18:46:38

标签: android eclipse calculator textwatcher

我有以下代码,我需要更改一点程序。因为它是有效的,当我按下b1按钮返回我在文本字段et3上计算3个其他文本字段。我需要在不按下按钮的情况下返回文本字段et3上的计算。我想我需要在文本字段et4上添加一个观察者,当这不是空时,然后返回我对et3的计算。代码就是这个

package com.example.b15_calc;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
EditText et1, et2, et3, et4;
Button b1, b2;

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

    et1 = (EditText) findViewById(R.id.editText1);
    et2 = (EditText) findViewById(R.id.editText2);
    et3 = (EditText) findViewById(R.id.editText3);
    et4 = (EditText) findViewById(R.id.editText4);

    b1 = (Button) findViewById(R.id.button1);
    b2 = (Button) findViewById(R.id.button2);

            //step3 : write add functionality.
            b2.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                                et1.setText("");
                                et2.setText("");
                                et3.setText("");
                                et4.setText("");


                }
    });

    //step3 : ΜΕΤΑΒΛΗΤΕΣ
    b1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            String f = et1.getText().toString();
            double i = Double.parseDouble(f);
            String s = et2.getText().toString();
            double j = Double.parseDouble(s);
            String w = et4.getText().toString();
            double q = Double.parseDouble(w);
            double price_gold = 10;
            double fpa = 1.23;
            double fpol = 0.10;
            double fpolam = 999;
            double isot = 100;
            double sint_ker = 10;
            double result1 = (i * price_gold) + (j * 1000) + (q * isot);
            double result2 = result1 / 340.75;
            int gap;

            if (result2 >= fpolam){
                double result = (result2 * fpol);
                double result3 = ((result2 * sint_ker) * fpa) + result;
                if(result3 < 1000)
                {
                  gap = 10;       
                }
                else if(result3 < 5000)
                {
                  gap = 50;
                }
                else //5000+
                {
                  gap = 100;
                }
                int total = (int) Math.ceil(result3 / gap) * gap;




                String res = String.valueOf(total);
                et3.setText(res);
            } else{

                double result3 = ((result2 * sint_ker) * fpa);
                if(result3 < 1000)
                {
                  gap = 10;       
                }
                else if(result3 < 5000)
                {
                  gap = 50;
                }
                else //5000+
                {
                  gap = 100;
                }
                int total = (int) Math.ceil(result3 / gap) * gap;
                String res = String.valueOf(total);
                et3.setText(res);
            }




        }
    });




}

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

}

1 个答案:

答案 0 :(得分:0)

您可以使用TextWatcher

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

            // TODO Auto-generated method stub
        }

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

            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {

            // make the calculation here and set the result to et3

           String f = et1.getText().toString();
           double i = Double.parseDouble(f);
           String s = et2.getText().toString();
           double j = Double.parseDouble(s);
           String w = s.toString();
           double q = Double.parseDouble(w);
           double price_gold = 10;
           double fpa = 1.23;
           double fpol = 0.10;
           double fpolam = 999;
           double isot = 100;
           double sint_ker = 10;
           double result1 = (i * price_gold) + (j * 1000) + (q * isot);
           double result2 = result1 / 340.75;
           int gap;

           if (result2 >= fpolam){
               double result = (result2 * fpol);
               double result3 = ((result2 * sint_ker) * fpa) + result;
               if(result3 < 1000)
               {
                 gap = 10;       
               }
               else if(result3 < 5000)
               {
                 gap = 50;
               }
               else //5000+
               {
                 gap = 100;
               }
               int total = (int) Math.ceil(result3 / gap) * gap;




               String res = String.valueOf(total);
               et3.setText(res);
           } else{

               double result3 = ((result2 * sint_ker) * fpa);
               if(result3 < 1000)
               {
                 gap = 10;       
               }
               else if(result3 < 5000)
               {
                 gap = 50;
               }
               else //5000+
               {
                 gap = 100;
               }
               int total = (int) Math.ceil(result3 / gap) * gap;
               String res = String.valueOf(total);
               et3.setText(res);
           }
        }
    });