简体   繁体   中英

How to check if two EditText contain text

I have two EditText and one Button in my Layout. If the two EditText both contain a text, i want to enable the button:

@Override
 public boolean onKey(View v, int keyCode, KeyEvent event) {
  System.out.println("Key input");
  String usr = user.getText().toString();
  String pw = password.getText().toString();

  if ( (!usr.equals("")) && (!pw.equals(""))) {
   ok.setEnabled(true);
  } else {
   ok.setEnabled(false);
  }

  return true;
 }

So i tried to register several listeners on the EditText fields:

user.setOnKeyListener(focusListener);
password.setOnKeyListener(focusListener);

Unfortunately, the onKeyListener does not seem to work at all. So i also tried the FocusChangedListener and the ActionListener. But none of them provided the behaviour i'd like.

Any hints how to implement that?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM