Edittext和错误消息

时间:2017-07-09 12:03:27

标签: android android-edittext

我有9个edittext我的问题是当你在edittext中输入一个数字时我不能在其他8个edittext中输入另一个数字例如,如果我在editext中输入5,如果我想在8中输入另外5个edittext restants不会显示任何内容,将显示错误消息。

    et1.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 s) {

            tester();
        }
    });
    et2.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {

        }

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

        }

        @Override
        public void afterTextChanged(final Editable s) {
            tester();

        }

    });
    et3.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {

        }

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

        }

        @Override
        public void afterTextChanged(final Editable s) {
            tester1();

        }

    });
    et4.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {

        }

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

        }

        @Override
        public void afterTextChanged(final Editable s) {

        }

    });
    et5.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {

        }

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

        }

        @Override
        public void afterTextChanged(final Editable s) {

        }
    });

    et6.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {

        }

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

        }

        @Override
        public void afterTextChanged(final Editable s) {

        }
    });
    et7.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {

        }

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

        }

        @Override
        public void afterTextChanged(final Editable s) {

        }
    });
    et8.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {

        }

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

        }

        @Override
        public void afterTextChanged(final Editable s) {
            tester();

        }
    });
    et9.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {

        }

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

        }

        @Override
        public void afterTextChanged(final Editable s) {

        }
    })
protected void tester()

{
    if (et1.getText().toString().equals(et2.getText().toString()) ||
            et1.getText().toString().equals(et3.getText().toString()) ||
            et1.getText().toString().equals(et4.getText().toString()) ||
            et1.getText().toString().equals(et5.getText().toString()) ||
            et1.getText().toString().equals(et6.getText().toString()) ||
            et1.getText().toString().equals(et7.getText().toString()) ||
            et1.getText().toString().equals(et8.getText().toString()) ||
            et1.getText().toString().equals(et9.getText().toString())) {
        Toast.makeText(this, "vous ne devez utiliser ce nombre deux fois ", Toast.LENGTH_SHORT).show();

    }
}

2 个答案:

答案 0 :(得分:0)

您可以将编辑文本中的文本存储到数组中。在每次文本更改时,您都可以检查它是否存在冲突。如果是,请举杯祝酒。相应地更新textInputs字符串。不确定代码是否有效,但希望你能得到这个想法。

声明一个检查数组

String[] textInputs = new String[9];

使用其他输入检查输入

void checkTextValid(String input){
   if (input.length > 0 && ArrayUtils.contains( textInputs, input ) ) {
       Toast.makeText(this, "vous ne devez utiliser ce nombre deux fois ", Toast.LENGTH_SHORT).show();
   }
}

void updateInputs(int id, String s){
    textInputs[id] = s;
}

checkTextValid(s.toString());添加到每个onTextChanged()。

updateInputs(0,e.toString());添加到每个afterTextChanged()。

答案 1 :(得分:0)

使用此

public class MainActivity extends AppCompatActivity {


    EditText edt_1,edt_2,edt_3,edt_4,edt_5;

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

        edt_1 = (EditText)findViewById(R.id.edt_1);
        edt_2 = (EditText)findViewById(R.id.edt_2);
        edt_3 = (EditText)findViewById(R.id.edt_3);
        edt_4 = (EditText)findViewById(R.id.edt_4);
        edt_5 = (EditText)findViewById(R.id.edt_5);


        editTextWatcher(edt_1);
        editTextWatcher(edt_2);
        editTextWatcher(edt_3);
        editTextWatcher(edt_4);
        editTextWatcher(edt_5);
    }




    public void editTextWatcher(final EditText edt) {
        edt.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 s) {

                edt.removeTextChangedListener(this);
                edt.setText("");
                edt.addTextChangedListener(this);

                ArrayList<String> texts = new ArrayList<String>();
                texts.add(edt_1.getText().toString());
                texts.add(edt_2.getText().toString());
                texts.add(edt_3.getText().toString());
                texts.add(edt_4.getText().toString());
                texts.add(edt_5.getText().toString());

                for (String text : texts){
                    if(s.toString().equals(text)){
                        Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
                        edt.removeTextChangedListener(this);
                        edt.setText("");
                        edt.addTextChangedListener(this);
                        return;
                    }
                }

                edt.removeTextChangedListener(this);
                edt.setText(s.toString());
                edt.addTextChangedListener(this);


            }
        });
    }

}