1

我希望当用户在我的应用程序中注册用户名时,如果该用户名已经被使用,他/她会得到一个祝酒词“用户名已经被使用”。

我已经在我的数据库中包含了 UNIQUE 约束,它工作正常,但在活动本身中,我想让用户知道他们尝试注册的用户名已经存在。

    package com.fullfrontalgames.numberfighter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Register extends Activity {
    EditText UsernameReg, PasswordReg, PasswordConfirm, EmailReg;

    Button Register;

    DBAdapter db;

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

        // get Instance of Database Adapter
        final DBAdapter db = new DBAdapter(this);
        db.open();

        // Get reference of views.
        UsernameReg = (EditText)findViewById(R.id.UsernameReg);
        PasswordReg = (EditText)findViewById(R.id.PasswordReg);
        EmailReg = (EditText)findViewById(R.id.EmailReg);
        PasswordConfirm = (EditText)findViewById(R.id.PasswordConfirm);

        Register = (Button)findViewById(R.id.Register);
        Register.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String Username = UsernameReg.getText().toString();
                String Password = PasswordReg.getText().toString();
                String PassConfirm = PasswordConfirm.getText().toString();
                String Email = EmailReg.getText().toString();
                String TakenUsername = db.getData();

                // Check if any of the field are vacant.
                if (Username.equals("") || Password.equals("") || PassConfirm.equals("")) {
                    Toast.makeText(getApplicationContext(), "Field Vacant", Toast.LENGTH_LONG)
                            .show();
                    return;
                }
                // Check if both passwords matches
                if (!Password.equals(PassConfirm)) {
                    Toast.makeText(getApplicationContext(), "Password does not match",
                            Toast.LENGTH_LONG).show();
                    return;
                }
                if (Username.equals(TakenUsername))

                {
                    Toast.makeText(getApplicationContext(), "Username Already Taken",
                            Toast.LENGTH_LONG).show();
                    return;
                } else {
                    // Save data in database
                    db.insertPlayer(Username, Password, Email);
                    Toast.makeText(getApplicationContext(), "Account Successfully Created ",
                            Toast.LENGTH_LONG).show();
                    startActivity(new Intent("com.fullfrontalgames.numberfighter.Login"));
                }
            }

        });

    }

}
4

2 回答 2

2

代替

if (Username.equals(TakenUsername))

            {
                Toast.makeText(getApplicationContext(), "Username Already Taken",
                        Toast.LENGTH_LONG).show();
                return;
            } else {
                // Save data in database
                db.insertPlayer(Username, Password, Email);
                Toast.makeText(getApplicationContext(), "Account Successfully Created ",
                        Toast.LENGTH_LONG).show();
                startActivity(new Intent("com.fullfrontalgames.numberfighter.Login"));
            }  

你可以这样做

long id = db.insertPlayer(Username, Password, Email);
if (id == -1)
{
    Toast.makeText(getApplicationContext(), "Username Already Taken",
                        Toast.LENGTH_LONG).show();
}  

在哪里

public long insertPlayer(String Username, String Password, String Email)
{
    ContentValues values = new ContentValues();
    values.put("USERNAME", Username);
    values.put("PASSWORD", Password);
    values.put("EMAIL", Email);

    return db.insertWithOnConflict(Table name, null, values, SQLiteDatabase.CONFLICT_IGNORE);  

}
于 2013-04-22T03:32:09.560 回答
0

如果用户名存在,请检查您的数据库表。您可以执行以下操作

Cursor c = db.query(TABLE_NAME, new String[] { USERNAME},
        USERNAME+ " ='" + userName + "'", null, null, null, null);
        if(c.getCount > 0)
               username taken;;
于 2013-04-22T03:18:14.823 回答