Android – EditText onchangelistener function

I have the following code:

nameOfInf.setOnFocuschangelistener(new OnFocuschangelistener() {
    if (strTollAmount.length() > 0) {
        nameOfInf.setBackgroundColor(getResources().getColor(android.R.color.white));
        nameOfInf.setTextColor(getResources().getColor(android.R.color.black));
    }
});
tollAmount.setOnFocuschangelistener(new OnFocuschangelistener() {
    if (strInfName.length() > 0) {
        tollAmount.setBackgroundColor(getResources().getColor(android.R.color.white));
        tollAmount.setTextColor(getResources().getColor(android.R.color.black));
    }
});

This function checks whether the value on the text box is empty or null. Therefore, if the user enters content in the text box, the background and foreground colors should change. But that doesn't happen. Do you know how to edit it?

resolvent:

I think what you are looking for is textwatcher (although onfocuschanged may be useful, it is another method). Here is the sample code:

TextWatcher watcher= new TextWatcher() {
        public void afterTextChanged(Editable s) { 
            if (Text@R_942_2419@1.getText().toString().equals("")) {
                 Text@R_942_2419@1.setBackgroundColor(getResources().getColor(android.R.color.white));
                 Text@R_942_2419@1.setTextColor(getResources().getColor(android.R.color.black));
            }

        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
              //Do something or nothing.                
        }
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //Do something or nothing
        }
    };

    Text@R_942_2419@1.addTextChangedListener(watcher);

You also need to use. Equals () for string comparisons

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>