结合使用Android TextWatcher和多个用户输入值并更新这些值 [英] Using Android TextWatcher with multiple user input values and Update thses values

查看:97
本文介绍了结合使用Android TextWatcher和多个用户输入值并更新这些值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我也是Android的新手。我尝试开发一个Android应用程序以获取用户输入项的总数。这是我的应用程序的草图。在此处输入图像描述

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

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

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

    }
} 


推荐答案

希望获得帮助。

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

这篇关于结合使用Android TextWatcher和多个用户输入值并更新这些值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆