如何在Android中控制EditText,不要两次输入相同的数字?

时间:2015-03-19 16:12:21

标签: android android-edittext textwatcher charsequence

我尝试使用TextChangedListener,beforeTextChanged和afterTextChanged!但我没有结果

2 个答案:

答案 0 :(得分:1)

TextWatcher课程可以帮助您:

   @Override
      public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub
      }
      @Override
      public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
          int arg3) {
        // TODO Auto-generated method stub
      }
      @Override
      public void onTextChanged(CharSequence s, int a, int b, int c) {
        // TODO Auto-generated method stub
        output.setText(s);
        if(a == 9){
          Toast.makeText(getApplicationContext(), "Maximum Limit Reached", Toast.LENGTH_SHORT).show();
        }
      }};

Complete reference could be found here

答案 1 :(得分:0)

这项工作对我来说:)

txtwt = new TextWatcher()
    {
       char last ;
       @Override
       public void beforeTextChanged(CharSequence s, int start, int count, int after)
       {
           if (s.toString().length() >= 1)
                EntredNumber = s.toString();
        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {
            if (s.toString().length() >= 2)
            {
                last = (s.toString().charAt(s.toString().length() - 1));
               // Toast.makeText(getApplicationContext(), last +"", Toast.LENGTH_SHORT).show();
                if (EntredNumber.contains((last + "")))
                {
                    txtImputNumber.removeTextChangedListener(txtwt);
                    txtImputNumber.setText(EntredNumber);
                    txtImputNumber.setSelection(EntredNumber.length());
                    txtImputNumber.addTextChangedListener(txtwt);
                }
            }
        }
        @Override
        public void afterTextChanged(Editable s)
        {
        }
    };

   txtImputNumber.addTextChangedListener(txtwt);