Domanda

I have looked at some other questions and posts, but I can't seem to figure this out... I am building a conversion app, I am able to auto populate Feet and Yards by putting the inches in, using addTextChangedListener, but I cant figure out how to addTextChangedListener to the other fields so that I can enter Feet and automatically convert to Inches and Yards, and Yards to Inches and Feet...

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

       final EditText editIn = (EditText) findViewById(R.id.editIn);
       final EditText editFt = (EditText) findViewById(R.id.editFt);
       final EditText editYd = (EditText) findViewById(R.id.editYd);

       editIn.addTextChangedListener(new TextWatcher() {
           @Override
           public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) { }
           @Override
           public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
               try {
                   double in = Double.valueOf(editIn.getText().toString());
                   double ft = in / 12;
                   double yd = in / 36;
                   editFt.setText(String.valueOf(ft));
                   editYd.setText(String.valueOf(yd));
               } catch (NumberFormatException e) {
                   e.printStackTrace();
               }
           }
           @Override
           public void afterTextChanged(Editable editable) { }
È stato utile?

Soluzione

    TextWatcher watcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //YOUR CODE
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            //YOUR CODE
        }

        @Override
        public void afterTextChanged(Editable s) {

                //YOUR CODE
        }
    };

    editIn.addTextChangedListener(watcher);
    editFt.addTextChangedListener(watcher);
    editYd.addTextChangedListener(watcher);

Altri suggerimenti

hey since you asked for different ways....i came up with a way to use a textwatcher to validate different fields and each field can have its own validation.

/* A TextWatcher which can be reused anonymously only.
 *  
 */
public abstract class MyTextWatcher implements TextWatcher {

    private TextView view;


    // view represents the view you want to watch. Should inherit from
    // TextView
    private MyTextWatcher(View view) {

        if (view instanceof TextView){
            this.view = (TextView) view;
        }
        else
            throw new ClassCastException(
                    "view must be an instance Of TextView");
    }

    /**
     * 
     * @param view - the textview to validate
     * override this to handle any textview validations and UI error handling
     */

     public abstract void validate(TextView view);



    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        validate(); //this calls all the different views validate() methods

    }

    @Override
    public void afterTextChanged(Editable editable) {           
    }

    public void forceValidation(){
        validate(view);

    }

    public void markAsErrorField(){
         if(isEmptyField()){

             view.setHintTextColor(getResources().getColorStateList(R.color.red));

         }
    }

    //resets textviews to default appearance
    public void markAsDefault(){
             view.setHintTextColor(getResources().getColorStateList(R.color.default));
    }


    public boolean isEmptyField() {
        return view.getText().toString().trim().equals("");
    }

}

afterwards, i can use the above anonymously to set different validation methods i want to run. IN the above i have markAsDefault and markAsErrorField methods ...heres how to apply them:

et1.addTextChangedListener(new MyTextWatcher(et1){
        @Override
        public void validate(TextView view) {

            markAsErrorField();
        }
    });

et2.addTextChangedListener(new MyTextWatcher(et2){
            @Override
            public void validate(TextView view) {

                markAsDefault();
            }
        });

as an extra: if you want to use the forceValidate method then use a static arraylist in outer class and in textwatcher constructor save each instance to it. Then have for loop that for each textwatcher stored call forceValidate();

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top