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

偶尔善良 提交于 2020-01-06 03:21:07

问题


I am very new too Android. I try to develop an android application to get the total amount of user input items. Here is a sketch of my application.enter image description here

User Must enter First Row Col1 and Col 2. But First Row Col3 can enter or not. In Sub 1 textview the total value of First Row should display. This value should also display in Result textview. Likewise if user inserts data to Second Row, values must enter to under Col1 and Col2. If user likes, can enter value to under Col 3. Second Row total value Should display in Sub 2 textview. If User Enter Values to Second Row, then the Result textview should automatically update to the total value of Sub 1 and Sub 2 textviews. I try to do this as follows, But I cannot find a correct way to do this. Please if someone helps me to solve this, I really thankful. This is the java class

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));

    }
} 

回答1:


Hope this help.

/**
 * 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);
}



回答2:


This is a textWatcher Metod

....
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
        }
    };
}


来源:https://stackoverflow.com/questions/46167241/using-android-textwatcher-with-multiple-user-input-values-and-update-thses-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!