使用具有多个用户输入值的 Android TextWatcher 并更新这些值

Using Android TextWatcher with multiple user input values and Update thses values

我也是新人Android。我尝试开发一个 android 应用程序来获取用户输入项的总量。这是我的应用程序的草图。enter image description here

用户必须输入First Row Col1和Col 2。但是First Row Col3可以输入也可以不输入。在 Sub 1 textview 中,第一行的总值应该显示。该值也应显示在结果文本视图中。同样,如果用户将数据插入第二行,则值必须输入到 Col1 和 Col2 下。如果用户愿意,可以在第 3 列下输入值。第二行总值应显示在 Sub 2 文本视图中。如果用户在第二行输入值,则结果文本视图应自动更新为 Sub 1 和 Sub 2 文本视图的总值。我尝试按如下方式执行此操作,但我找不到正确的方法来执行此操作。如果有人帮助我解决这个问题,我真的很感激。 这是javaclass

public class AddTwo extends AppCompatActivity {

    EditText edit1, edit2, edit3;
    EditText edit4, edit5, edit6;
    TextView textViewSub1, textViewSub2, textViewResult;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.activity_add_two);

        /*First row variables*/
        edit1 = (EditText) findViewById(R.id.editText1);
        edit2 = (EditText) findViewById(R.id.editText2);
        edit3 = (EditText) findViewById(R.id.editText3);
        textViewSub1 = (TextView) findViewById(R.id.TextViewsub1);


        /*Second row variables*/
        edit4 = (EditText) findViewById(R.id.editText5);
        edit5 = (EditText) findViewById(R.id.editText6);
        edit6 = (EditText) findViewById(R.id.editText7);
        textViewSub2 = (TextView) findViewById(R.id.TextViewsub2);

        /*Final Total TextView variable*/
        textViewResult = (TextView) findViewById(R.id.textView_result);


        //use TextWatcher for edit1 in First Row Col1
        edit1.addTextChangedListener(new TextWatcher() {

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // TODO Auto-generated method stub
            }

            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //textViewResult.setText(addNumbers());
            }

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

        //use TextWatcher for first First Row Col2
        edit2.addTextChangedListener(new TextWatcher() {

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // TODO Auto-generated method stub
            }

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

            }

            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                //textViewResult.setText(addNumbers());
            }
        });

        //use TextWatcher for first First Row Col3
        edit3.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

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

            @Override
            public void afterTextChanged(Editable editable) {
                textViewResult.setText(addNumbers());
            }
        });

    }


    //Define method addString()
    private String addNumbers() {

        //declaring variables
        int number1, number2;
        double num1, num2;
        double d1, d2;
        double da1, da2;

        Double orderTotal1 = null, orderTotal2 = null;
        Double ordTot1 = null, ordTot2 = null;


        DecimalFormat df = new DecimalFormat("0.00##");

        //For First Row
        /*if only the first and second edittext fields in First Row is filled*/
        if ((edit1.getText().toString() != "" && edit1.getText().length() > 0) && (edit2.getText().toString() != "" && edit2.getText().length() > 0)) {

            num1 = Double.parseDouble((edit1.getText().toString()));
            number1 = Integer.parseInt(edit2.getText().toString());
            ordTot1 = num1 * number1;
            textViewSub1.setText(df.format(ordTot1));

         /*else if the first,secondn third (all) edittext fields in First Row is filled*/
        } else if ((edit1.getText().toString() != "" && edit1.getText().length() > 0) && (edit2.getText().toString() != "" && edit2.getText().length() > 0) && (edit3.getText().toString() != "" && edit3.getText().length() > 0)) {

            num1 = Double.parseDouble((edit1.getText().toString()));
            number1 = Integer.parseInt(edit2.getText().toString());
            d1 = Double.parseDouble(edit3.getText().toString());

            da1 = 100 - d1;
            orderTotal1 = ((da1 * number1) * num1) / 100;
            textViewSub1.setText(df.format(orderTotal1));

        } else {
            number1 = (int) 0.00;
            num1 = 0.00;
            da1 = 0.00;
        }

        //For the Second Row
        /*if only the first and second edittext fields in Second Row is filled*/
        if ((edit4.getText().toString() != "" && edit4.getText().length() > 0) && (edit5.getText().toString() != "" && edit5.getText().length() > 0)) {

            num2 = Double.parseDouble((edit4.getText().toString()));
            number2 = Integer.parseInt(edit5.getText().toString());
            ordTot2 = num2 * number2;
            textViewSub2.setText(df.format(ordTot2));

         /*else if the first,secondn third (all) edittext fields in Second Row is filled*/
        } else if ((edit4.getText().toString() != "" && edit4.getText().length() > 0) && (edit5.getText().toString() != "" && edit5.getText().length() > 0) && (edit6.getText().toString() != "" && edit6.getText().length() > 0)) {
            num2 = Double.parseDouble((edit1.getText().toString()));
            number2 = Integer.parseInt(edit2.getText().toString());
            d2 = Double.parseDouble(edit3.getText().toString());

            da2 = 100 - d2;
            orderTotal2 = ((da2 * number2) * num2) / 100;
            textViewSub2.setText(df.format(orderTotal2));

        } else {
            number2 = (int) 0.00;
            num2 = 0.00;
            da2 = 0.00;
        }

       //return statement
        return df.format((orderTotal1) + (orderTotal2));

    }
} 

希望对您有所帮助。

/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_add_two);

    /*First row variables*/
    edit1 = (EditText) findViewById(R.id.editText1);
    edit2 = (EditText) findViewById(R.id.editText2);
    edit3 = (EditText) findViewById(R.id.editText3);
    textViewSub1 = (TextView) findViewById(R.id.TextViewsub1);


    /*Second row variables*/
    edit4 = (EditText) findViewById(R.id.editText5);
    edit5 = (EditText) findViewById(R.id.editText6);
    edit6 = (EditText) findViewById(R.id.editText7);
    textViewSub2 = (TextView) findViewById(R.id.TextViewsub2);

    /*Final Total TextView variable*/
    textViewResult = (TextView) findViewById(R.id.textView_result);


    edit1.addTextChangedListener(new LashCustomTextWatcher());
    edit2.addTextChangedListener(new LashCustomTextWatcher());
    edit3.addTextChangedListener(new LashCustomTextWatcher());
    edit4.addTextChangedListener(new LashCustomTextWatcher());
    edit5.addTextChangedListener(new LashCustomTextWatcher());
    edit6.addTextChangedListener(new LashCustomTextWatcher());
}

public class LashCustomTextWatcher implements TextWatcher{

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        textViewResult.setText(lashCalculate());
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
}

public String lashCalculate(){
    //declaring variables
    double row1_value = 0;
    double row2_value = 0;

    DecimalFormat df = new DecimalFormat("0.00##");

    //calculate first row
    if (!edit1.getText().toString().equals("") && !edit2.getText().toString().equals("")){
        double num1 = Double.parseDouble((edit1.getText().toString()));
        double num2 = Double.parseDouble((edit2.getText().toString()));

        row1_value = num1*num2;

        double num3 = 0;
        if (!edit3.getText().toString().equals("")){
            num3 = Double.parseDouble((edit3.getText().toString()));
            row1_value = (((100 - num3) * num2) * num1)/100;
        }

        textViewSub1.setText(df.format(row1_value));
    }

    //calculate second row
    if (!edit4.getText().toString().equals("") && !edit5.getText().toString().equals("")){
        double num4 = Double.parseDouble((edit4.getText().toString()));
        double num5 = Double.parseDouble((edit5.getText().toString()));

        row2_value = num4*num5;

        double num6 = 0;
        if (!edit6.getText().toString().equals("")){
            num6 = Double.parseDouble((edit6.getText().toString()));
            row2_value = (((100 - num6) * num5) * num4)/100;
        }

        textViewSub2.setText(df.format(row2_value));
    }

    return df.format(row1_value + row2_value);
}

这是一个 textWatcher 方法

....
public EditText edit1 ,edit2 ,edit3;
public void onCreate(Bundle savedInstanceState) {
.....
edit1 = (EditText) findViewById(R.id.editText1);
edit2 = (EditText) findViewById(R.id.editText2);
edit3 = (EditText) findViewById(R.id.editText3);

edit1.addTextChangedListener(textWatcher());
edit2.addTextChangedListener(textWatcher());
edit3.addTextChangedListener(textWatcher());
......

public TextWatcher textWatcher() {

    return new TextWatcher() {

        CountDownTimer timer = null;

        @Override
        public void afterTextChanged(Editable s) {
            //do some thigs
        }

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

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
       //do some thigs
        }
    };
}