即使密码和确认密码匹配,也会出现Toast消息

时间:2016-03-29 19:36:29

标签: java android authentication

package com.example.chris.thegiver;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterActivity extends AppCompatActivity {
    // calls the constructor of the DatabaseHelper class which will create the database and table
    DatabaseHelper registerDB = new DatabaseHelper(this);

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

    public void onRegisterClick(View v) {
        if (v.getId() == R.id.btnReg) {

            EditText etName = (EditText) findViewById(R.id.etName);
            EditText etEmail = (EditText) findViewById(R.id.etEmail);
            EditText etUsername = (EditText) findViewById(R.id.etUsername);
            EditText etPassword = (EditText) findViewById(R.id.etUsername);
            EditText etConfirm = (EditText) findViewById(R.id.etConfirm);


            String name = etName.getText().toString();
            String email = etEmail.getText().toString();
            String user = etUsername.getText().toString();
            String password = etPassword.getText().toString();
            String confirm = etConfirm.getText().toString();

            if (password.equals(confirm)) {
                Contact c = new Contact(); 
                c.setName(name);
                c.setEmail(email);
                c.setUsername(user);
                c.setPassword(password);

                registerDB.insertContact(c);
            }
            else {
                // Pop up message It skips the original if statement for some reason
                Toast pass = Toast.makeText(RegisterActivity.this, "Passwords Dont Match!", Toast.LENGTH_SHORT);
                pass.show();
            }

        }
    }
}

1 个答案:

答案 0 :(得分:1)

根据提供的代码,唯一可能的原因是这两个中的一个:

EditText etPassword = (EditText) findViewById(R.id.etUsername);
EditText etConfirm = (EditText) findViewById(R.id.etConfirm);

这些ID不是他们所说的。检查xml布局并确保这些edittext id实际上是您认为的那些。例如,由于复制粘贴,您可能已转换usernamepassword ID,但未注意到。

或者:您正在添加空格,尽管您没有看到它,但文本确实不同

编辑:请多关注一下。问题出在这里:

EditText etPassword = (EditText) findViewById(R.id.etUsername);
EditText etConfirm = (EditText) findViewById(R.id.etConfirm);

原因一被确认