2016-06-13 103 views
0

我只想這樣做如果我輸入一個等級,那麼它會根據輸入的等級更改爲點字段,如果我輸入一個點,那麼它會更改等級字段根據輸入的點。我爲editText添加了textChangeListener,但是當我輸入某些內容時,我的應用程序根本沒有響應。Android TextWatcher不能連續編輯兩個編輯文本

http://i.stack.imgur.com/50LYo.png

grade.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) { 

       if (grade.getText().toString().equals("A+")) 
       { 
        points.setText("4.00"); 
       } 
       else if (grade.getText().toString().equals("A")) 
       { 
        points.setText("3.75"); 
       } 
       else if (grade.getText().toString().equals("A-")) 
       { 
        points.setText("3.50"); 
       } 
       else if (grade.getText().toString().equals("B+")) 
       { 
        points.setText("3.25"); 
       } 
       else if (grade.getText().toString().equals("B")) 
       { 
        points.setText("3.00"); 
       } 
       else if (grade.getText().toString().equals("B-")) 
       { 
        points.setText("2.75"); 
       } 
       else if (grade.getText().toString().equals("C+")) 
       { 
        points.setText("2.50"); 
       } 
       else if (grade.getText().toString().equals("C")) 
       { 
        points.setText("2.25"); 
       } 
       else if (grade.getText().toString().equals("D")) 
       { 
        points.setText("2.00"); 
       } 
       else if (grade.getText().toString().equals("F")) 
       { 
        points.setText("0.0"); 
       } 
       else 
        points.setText(null); 

      } 

      @Override 
      public void afterTextChanged(Editable s) { 

      } 
     }); 

points.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) { 

       if (points.getText().toString().matches("[4]|[4]\\.[0]{0,2}")) 
       { 
        grade.setText("A+"); 
       } 
       else if (points.getText().toString().matches("[3]\\.[7][5]{1}")) 
       { 
        grade.setText("A"); 
       } 
       else if (points.getText().toString().matches("[3]\\.[5][0]{0,1}")) 
       { 
        grade.setText("A-"); 
       } 
       else if (points.getText().toString().matches("[3]\\.[2][5]{1}")) 
       { 
        grade.setText("B+"); 
       } 
       else if (points.getText().toString().matches("[3]|[3]\\.[0]{0,2}")) 
       { 
        grade.setText("B"); 
       } 
       else if (points.getText().toString().matches("[2]\\.[7][5]{1}")) 
       { 
        grade.setText("B-"); 
       } 
       else if (points.getText().toString().matches("[2]\\.[5][0]{0,1}")) 
       { 
        grade.setText("C+"); 
       } 
       else if (points.getText().toString().matches("[2]\\.[2][5]{1}")) 
       { 
        grade.setText("C"); 
       } 
       else if (points.getText().toString().matches("[2]|[2]\\.[0]{0,2}")) 
       { 
        grade.setText("D"); 
       } 
       else if (points.getText().toString().matches("[0]|[0]\\.[0]{0,2}")) 
       { 
        grade.setText("F"); 
       } 
       else 
        grade.setText(null); 

      } 

      @Override 
      public void afterTextChanged(Editable s) { 

      } 
     }); 
+1

分享您的代碼 –

+0

把你textChange聽者這裏 – Vickyexpert

+0

的代碼檢查一些錯誤來還是不來? – sushildlh

回答

0

嘗試以下代碼:

呼叫的setFocus在onViewCreated()在活動

的setFocus(級)片段或的onCreate()的方法;

setFocus(points);

TextWatcher gradeWatcher = 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) { 
      if (grade.getText().toString().equals("A+")) 
       { 
        points.setText("4.00"); 
       } 
       else if (grade.getText().toString().equals("A")) 
       { 
        points.setText("3.75"); 
       } 
       else if (grade.getText().toString().equals("A-")) 
       { 
        points.setText("3.50"); 
       } 
       else if (grade.getText().toString().equals("B+")) 
       { 
        points.setText("3.25"); 
       } 
       else if (grade.getText().toString().equals("B")) 
       { 
        points.setText("3.00"); 
       } 
       else if (grade.getText().toString().equals("B-")) 
       { 
        points.setText("2.75"); 
       } 
       else if (grade.getText().toString().equals("C+")) 
       { 
        points.setText("2.50"); 
       } 
       else if (grade.getText().toString().equals("C")) 
       { 
        points.setText("2.25"); 
       } 
       else if (grade.getText().toString().equals("D")) 
       { 
        points.setText("2.00"); 
       } 
       else if (grade.getText().toString().equals("F")) 
       { 
        points.setText("0.0"); 
       } 
       else 
        points.setText(null); 

      } 
     }; 

TextWatcher pointWatcher = 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) { 
      if (points.getText().toString().matches("[4]|[4]\\.[0]{0,2}")) 
       { 
        grade.setText("A+"); 
       } 
       else if (points.getText().toString().matches("[3]\\.[7][5]{1}")) 
       { 
        grade.setText("A"); 
       } 
       else if (points.getText().toString().matches("[3]\\.[5][0]{0,1}")) 
       { 
        grade.setText("A-"); 
       } 
       else if (points.getText().toString().matches("[3]\\.[2][5]{1}")) 
       { 
        grade.setText("B+"); 
       } 
       else if (points.getText().toString().matches("[3]|[3]\\.[0]{0,2}")) 
       { 
        grade.setText("B"); 
       } 
       else if (points.getText().toString().matches("[2]\\.[7][5]{1}")) 
       { 
        grade.setText("B-"); 
       } 
       else if (points.getText().toString().matches("[2]\\.[5][0]{0,1}")) 
       { 
        grade.setText("C+"); 
       } 
       else if (points.getText().toString().matches("[2]\\.[2][5]{1}")) 
       { 
        grade.setText("C"); 
       } 
       else if (points.getText().toString().matches("[2]|[2]\\.[0]{0,2}")) 
       { 
        grade.setText("D"); 
       } 
       else if (points.getText().toString().matches("[0]|[0]\\.[0]{0,2}")) 
       { 
        grade.setText("F"); 
       } 
       else 
        grade.setText(null); 

      } 
     }; 
/*---------- SET FOCUS LISTENERS ON EDIT TEXT--------------------------*/ 
    public void setFocus(EditText editText){ 
     editText.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
      @Override 
      public void onFocusChange(View v, boolean hasFocus) { 
       if (hasFocus) { 
        if (v.getId() == grade.getId()) {  
         grade.addTextChangedListener(gradeWatcher); 
         points.removeTextChangedListener(pointWatcher); 
        } else if (v.getId() == points.getId()) { 
         points.addTextChangedListener(pointWatcher);  
         grade.removeTextChangedListener(gradeWatcher); 

        } 
       } 
      } 
     }); 
    }