简体   繁体   中英

Android Login and Register

I have created and app that allows the user to register a username and password, save it to a database and then login to a system. I thought the code was correct and there is no errors, but i don't think my buttons are working to actually carry out the tasks. Below is my code:

Main Activity

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

TextView tvRegisterLink;


DatabaseHelper helper = new DatabaseHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tvRegisterLink = (TextView) findViewById(R.id.tvRegisterLink);

    tvRegisterLink.setOnClickListener(this);
}

public void onButtonClick(View v)
{
    if(v.getId() == R.id.bLogin)
    {
        EditText a = (EditText) findViewById(R.id.etUsername);
        String str = a.getText().toString();
        EditText b = (EditText) findViewById(R.id.etPassword);
        String pass = b.getText().toString();

        String password = helper.searchPass(str);
        if(pass.equals(password))
        {
            Intent i = new Intent(MainActivity.this, Options.class);
            i.putExtra("Username",str);
            startActivity(i);
        }
        else
        {
            Toast temp = Toast.makeText(MainActivity.this , "Username and Password Dont Match!" , Toast.LENGTH_SHORT);
            temp.show();
        }
    }
}

@Override
public void onClick(View v) {
    switch(v.getId()){
        case R.id.tvRegisterLink:
            startActivity(new Intent(this, register.class));

            break;
    }
}

}

Register Class:

public class register extends AppCompatActivity {

DatabaseHelper helper = new DatabaseHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);
}

public void onbRegisterClick(View v)
{
    if(v.getId() == R.id.bRegister)
    {
        EditText username = (EditText) findViewById(R.id.RegUsername);
        EditText password = (EditText) findViewById(R.id.RegPassword);
        EditText password2 = (EditText) findViewById(R.id.RegPassword2);

        String usernamestr = username.getText().toString();
        String passwordstr = password.getText().toString();
        String password2str = password2.getText().toString();

        if(!passwordstr.equals(password2str))
        {
            //pop-up message
            Toast pass = Toast.makeText(register.this , "Passwords Do Not Match!" , Toast.LENGTH_SHORT);
            pass.show();
        }
        else
        {
            //insert the details in the database
            Contact c = new Contact();
            c.setUsername(usernamestr);
            c.setPassword(passwordstr);

            helper.insertContact(c);
        }
    }
}
}

Im fairly certain it has something to do with the onButtonClick method in the main activity and the onbRegisterClick method in the register because they are both saying that the methods are not being used. Any help at all is greatly appreciated!

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical">

<TextView
    android:layout_width="wrap_content"
    android:text="Username"
    android:layout_height="wrap_content" />

<EditText
    android:id="@+id/etUsername"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<TextView
    android:layout_width="wrap_content"
    android:text="Password"
    android:layout_height="wrap_content" />

<EditText
    android:id="@+id/etPassword"
    android:layout_width="match_parent"
    android:inputType="textPassword"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/bLogin"
    android:text="Login"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/tvRegisterLink"
    android:layout_width="wrap_content"
    android:text="Register Here"
    android:textStyle="bold"
    android:layout_gravity="center_horizontal"
    android:layout_height="wrap_content" />

</LinearLayout>

在您的xml中,将textview属性设置为true可单击,并且应该可以正常工作。

Put your

EditText a = (EditText) findViewById(R.id.etUsername);
EditText b = (EditText) findViewById(R.id.etPassword);

and

EditText username = (EditText) findViewById(R.id.RegUsername);
EditText password = (EditText) findViewById(R.id.RegPassword);
EditText password2 = (EditText) findViewById(R.id.RegPassword2);

in your onCreate method.

You could do like this:

EditText a, b;

**EIDT**
Button btnLogin;

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

a = (EditText) findViewById(R.id.etUsername);
b = (EditText) findViewById(R.id.etPassword);

**EIDT**
btnLogin = (Button) findViewById(R.id.bLogin);


btnLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


    String str = a.getText().toString();

    String pass = b.getText().toString();

    String password = helper.searchPass(str);
    if(pass.equals(password))
    {
        Intent i = new Intent(MainActivity.this, Options.class);
        i.putExtra("Username",str);
        startActivity(i);
    }
    else
    {
        Toast temp = Toast.makeText(MainActivity.this , "Username and Password Dont Match!" , Toast.LENGTH_SHORT);
        temp.show();
    }

  }
}

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