EditText 内容验证

EditText contents validation

我正在验证我的 EditText 内容,

我使用

验证我的 EditText 内容
String enteredUserName = userName.getText().toString();
String enteredPassword = userPassword.getText().toString();

if( enteredUserName != " " && enteredPassword != " " ) {
    createNewAccount();
}

但以上对我不起作用, 然后我看到了这个的解决方案,

if( enteredUserName.length() != 0 && enteredPassword.length() != 0 ) {
    createNewAccount();
}

它非常适合我

我的问题是,为什么我的第一个代码片段不起作用,而我在 java 中使用上面的代码进行验证。 谢谢

编辑2

setContentView( R.layout.create_account_activity_layout );
btn = ( Button ) findViewById( R.id.create_account_btn );
userName = ( EditText ) findViewById( R.id.name );
userPassword = ( EditText ) findViewById( R.id.password );

enteredUserName = userName.getText().toString().trim();
enteredPassword = userPassword.getText().toString().trim();

userName.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( enteredUserName.trim().length() != 0 && enteredPassword.trim().length() != 0){
                   createNewAccount();
               }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

userPassword.addTextChangedListener(new TextWatcher() {     
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub
         if( enteredUserName.trim().length() != 0 && enteredPassword.trim().length() != 0){
               createNewAccount();
           }
    }

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

    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub  
    }
});

编辑3

String enteredUserName; 
String enteredPassword;

@Override
protected void onCreate( Bundle savedInstanceState ) {
    // TODO Auto-generated method stub
    super.onCreate( savedInstanceState );

    setContentView( R.layout.create_account_activity_layout );
    btn = ( Button ) findViewById( R.id.create_account_btn );
    userName = ( EditText ) findViewById( R.id.name );
    userPassword = ( EditText ) findViewById( R.id.password );

    enteredUserName = userName.getText().toString().trim();
    enteredPassword = userPassword.getText().toString().trim();

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

                enteredUserName = userName.getText().toString();
                enteredPassword = userPassword.getText().toString();
                 if( enteredUserName.trim().length() != 0 && enteredPassword.trim().length() != 0){
                       createNewAccount();
                 }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    userPassword.addTextChangedListener(new TextWatcher() {         
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
             enteredUserName = userName.getText().toString();
             enteredPassword = userPassword.getText().toString();
             if( enteredUserName.trim().length() != 0 && enteredPassword.trim().length() != 0){
                   createNewAccount();
               }
        }

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

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    });

    /*while( enteredUserName.toString().isEmpty() || enteredPassword.toString().isEmpty() ) {
        Toast.makeText(CreateAccountActivity.this, "Please enter proper user name and password", Toast.LENGTH_SHORT).show();
        userName = ( EditText ) findViewById( R.id.name );
        userPassword = ( EditText ) findViewById( R.id.password );
        enteredUserName = userName.getText().toString().trim(); 
        enteredPassword = userPassword.getText().toString().trim();
    }*/
    /*  while( userName.getText().toString().equals("")  && userPassword.getText().toString().equals("") ) {
        Toast.makeText(CreateAccountActivity.this, "Please enter proper user name and password", Toast.LENGTH_SHORT).show();        
    }*/
    btn.setOnClickListener( this );
}

编辑4

protected void onCreate( Bundle savedInstanceState ) {
    // TODO Auto-generated method stub
    super.onCreate( savedInstanceState );

    setContentView( R.layout.create_account_activity_layout );
    btn = ( Button ) findViewById( R.id.create_account_btn );
    userName = ( EditText ) findViewById( R.id.name );
    userPassword = ( EditText ) findViewById( R.id.password );

    //enteredUserName = userName.getText().toString().trim();
    //enteredPassword = userPassword.getText().toString().trim();

    TextWatcher textWatcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String enteredUserName = userName.getText().toString();
            String enteredPassword = userPassword.getText().toString();
            if (enteredUserName.trim().length() != 0 && enteredPassword.trim().length() != 0) {
                Toast.makeText(CreateAccountActivity.this, "test", Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };
    userName.addTextChangedListener(textWatcher);
    userPassword.addTextChangedListener(textWatcher);

    /*
    userName.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) {

             enteredUserName = userName.getText().toString();
             enteredPassword = userPassword.getText().toString();
             if( enteredUserName.trim().length() != 0 && enteredPassword.trim().length() != 0){
                   createNewAccount();
             }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

    userPassword.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
             enteredUserName = userName.getText().toString();
             enteredPassword = userPassword.getText().toString();
             if( enteredUserName.trim().length() != 0 && enteredPassword.trim().length() != 0){
                   createNewAccount();
               }
        }

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

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    });
    */
    /*while( enteredUserName.toString().isEmpty() || enteredPassword.toString().isEmpty() ) {
        Toast.makeText(CreateAccountActivity.this, "Please enter proper user name and password", Toast.LENGTH_SHORT).show();
        userName = ( EditText ) findViewById( R.id.name );
        userPassword = ( EditText ) findViewById( R.id.password );
        enteredUserName = userName.getText().toString().trim(); 
        enteredPassword = userPassword.getText().toString().trim();
    }*/
    /*  while( userName.getText().toString().equals("")  && userPassword.getText().toString().equals("") ) {
        Toast.makeText(CreateAccountActivity.this, "Please enter proper user name and password", Toast.LENGTH_SHORT).show();
    }*/
    btn.setOnClickListener( this );
}

您通过 = 进行了字符串比较,这是不可能的 - 只需使用

if(!enteredUserName.equals("") && !enteredPassword.equals("")) 

要比较字符串,您必须这样做:

  if( !enteredUserName.equals(" ") && !enteredPassword.equals(" ") ) {
      createNewAccount();
 }

如果大写字母不重要,则始终使用 equals() 或 equalsIgnoreCase() 方法比较字符串

试试这个

String enteredUserName = userName.getText().toString();
     String enteredPassword = userPassword.getText().toString();

     if( !enteredUserName.equals("") && !enteredPassword.equals("") ) {
          createNewAccount();
     }

另外

阅读此文档http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

其他解决方案

String enteredUserName = userName.getText().toString();
         String enteredPassword = userPassword.getText().toString();

         if( !enteredUserName.isEmpty() && !enteredPassword.isEmpty() ) {
              createNewAccount();
         }

您的第一个解决方案无效,因为您尝试将文本实例与仅包含一个 space.

的字符串实例进行比较

第二个解决方案可行,但如果用户将写入 space 条件将被验证,因此在检查条件之前您应该 trim() 您的字符串,如下面的代码所示:

if( enteredUserName.trim().length() != 0 && enteredPassword.trim().length() != 0){
     createNewAccount();
}

trim() 方法从 String

中删除白色标记

或者您可以使用 String class

中的方法 isEmpty()

如果您想识别用户在 EditText 上更改文本的情况,您应该使用 TextWatcher,如下面的代码所示:

  editText.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) {
               String enteredUserName = userName.getText().toString();
               String enteredPassword = userPassword.getText().toString();
               if( enteredUserName.trim().length() != 0 && enteredPassword.trim().length() != 0){
                   createNewAccount();
               }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

按照我为 userNameuserPassword

显示的那样添加 TextWatcher

编辑

将您的 onCreate 方法更改为以下方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.create_account_activity_layout);
    Button btn = (Button) findViewById( R.id.create_account_btn );
    final EditText userName = (EditText) findViewById(R.id.name);
    final EditText userPassword = (EditText) findViewById(R.id.password);
    userName.addTextChangedListener(textWatcher);
    userPassword.addTextChangedListener(textWatcher);
    btn.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String enteredUserName = userName.getText().toString();
            String enteredPassword = userPassword.getText().toString();
            if (enteredUserName.trim().length() != 0 && enteredPassword.trim().length() != 0) {
                createNewAccount();
            }else{
                Toast.makeText(MainActivity2.this, "Fill both fields", Toast.LENGTH_LONG).show();
            }
        }
    });

}

您不能将字符串与 != 进行比较,您可以将其与 :

进行比较并不是一个好主意

-if(enteredUserName.equals("") && enteredPassword.equals(""))

-if(enteredUserName.toString().isEmpty() && enteredPassword.toString().isEmpty()

-if(enteredUserName.matches("") && enteredPassword.matches("")

有很多可能性可以比较,但我认为这些是最正确的。

希望对您有所帮助:)

编辑

如果您想使用长度,一种使用方式如下:

if (enteredUserName.getText().length() == 0) && (enteredPassword.getText().length() == 0)

EDIT2

试试这个逻辑,如果可行请告诉我。

 String enteredUserName = userName.getText().toString();
 String enteredPassword = userPassword.getText().toString();

 Boolean ok;
 ok = false;
 while (!ok) {
    if (!enteredUserName.equals("") && !enteredPassword.equals("")){
        ok = true;
        createNewAccount();
        //user and password are not empty or you can do your 
    }
    else {
        //do something to repeat this, because user or password is empty
    }
 }

试试这个:

 String enteredUserName = userName.getText().toString().trim();
 String enteredPassword = userPassword.getText().toString().trim();

 if( !enteredUserName.equalsIgnoreCase("")
      && !enteredPassword.equalsIgnoreCase("") )
 {
      createNewAccount();
 }

你应该使用

   if ( !TextUtils.isEmpty("enteredUserName")) {
     // createNewAccount();
    }