multiple edittext with only one textwatchers for calculation in android

有些话、适合烂在心里 提交于 2019-12-25 05:55:09

问题


I will get a value from first EditText and one more value from second EditText both values are calculated and show the result in third EditText which is using only one TextWatcher in android. Please help me thank you.

public class TextWatcher_Activity extends Activity {
    private EditText passwordEditText, passwordEditText1, passwordEditText2;
    private TextView textView;
    private View view;

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

        /* Initializing views */
        passwordEditText = (EditText) findViewById(R.id.password);
        textView = (TextView) findViewById(R.id.passwordHint);
        textView.setVisibility(View.GONE);

        passwordEditText1 = (EditText) findViewById(R.id.password1);
        passwordEditText2 = (EditText) findViewById(R.id.password2);

 passwordEditText.addTextChangedListener(passwordWatcher);
        passwordEditText1.addTextChangedListener(passwordWatcher);
        passwordEditText2.addTextChangedListener(passwordWatcher);  
}
     private final TextWatcher passwordWatcher = new TextWatcher() {
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            public void onTextChanged(CharSequence s, int start, int before, int count) {
                textView.setVisibility(View.VISIBLE);
            }

            public void afterTextChanged(Editable s) {


               *//* if (s.hashCode() == passwordEditText.getText().hashCode()) {
                    //Do else something with input.
                } else if (s.hashCode() == passwordEditText1.getText().hashCode()) {
                    //Do something else useful with input.
                }*//*

                switch (view.getId()) {
                    case R.id.password:
                        //doStuff(1);
                        if (s.length() == 0) {
                            textView.setVisibility(View.GONE);
                        } else {
                            textView.setText("You have entered : " + passwordEditText.getText());
                        }
                        break;
                    case R.id.password1:
                        //doStuff(2);
                        if (s.length() == 0) {
                            textView.setVisibility(View.GONE);
                        } else {
                            textView.setText("You have entered : " + passwordEditText1.getText());
                        }
                        break;
                    case R.id.password2:
                        if (s.length() == 0) {
                            textView.setVisibility(View.GONE);
                        } else {
                            textView.setText("You have entered : " + passwordEditText2.getText());
                        }
                        break;
                }
            }
        };

回答1:


You can use this method and ajust to your code

private void calculate(EditText editText1, EditText editText2, final EditText editText3) {
        final int[] value1 = {0};
        final int[] value2 = {0};
        final int[] total = {0};
        editText1.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) {
                value1[0] = Integer.parseInt(s.toString());
                total[0] = value1[0] + value2[0];
                editText3.setText(total[0]);
            }

            @Override
            public void afterTextChanged(Editable s) {}
        });
        editText2.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) {
                value2[0] = Integer.parseInt(s.toString());
                total[0] = value1[0] + value2[0];
                editText3.setText(total[0]);
            }

            @Override
            public void afterTextChanged(Editable s) {}
        });
    }

editText3 has total of editText1 + editText2



来源:https://stackoverflow.com/questions/42622845/multiple-edittext-with-only-one-textwatchers-for-calculation-in-android

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