EditText内容验证

时间:2015-03-13 08:18:04

标签: android android-edittext

我正在验证我的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中使用上面的代码进行验证。 感谢

EDIT2

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  
    }
});

EDIT3

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 );
}

Edit4

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 );
}

7 个答案:

答案 0 :(得分:2)

您的第一个解决方案无效,因为您尝试将文本实例与只包含一个空格的String实例进行比较。

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

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

trim()方法会从String

中删除白色标记

或者您可以使用String类

中的方法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();
            }
        }
    });

}

答案 1 :(得分:1)

比较字符串你必须这样做:

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

比较字符串总是使用方法equals()或equalsIgnoreCase()如果大写字母不重要

答案 2 :(得分:0)

你做了字符串比较=这是不可能的 - 只需使用

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

答案 3 :(得分:0)

试试这个

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();
         }

答案 4 :(得分:0)

您无法将字符串与!=进行比较,您不能将其与之比较:

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

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

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

有很多可比性,但那些是最正确的IMO。

希望有所帮助:)

修改

如果你想使用长度一种方式使用它就像:

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
    }
 }

答案 5 :(得分:0)

试试这个:

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

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

答案 6 :(得分:0)

您应该使用

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