dsrw29618
dsrw29618
2016-09-18 16:28

用户注册期间显示重复的用户名错误

已采纳

How can I create a dialog box or a toast on android that will tell the user that the username is already used by another user. Thanks guys. Here is my code for reference.

public class RegisterActivity extends AppCompatActivity {

    EditText etFirstname, etMiddlename, etLastname, etBirthDate, etAddress, etUsername, etPassword,etContactnumber,etConfirmPassword;
    Button btnRegister;
    Spinner spRole;
    int year_x,month_x,day_x;
    static final int DIALOG_ID = 0;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        showDialogOnButtonClick();
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        final Calendar cal = Calendar.getInstance();
        year_x = cal.get(Calendar.YEAR);
        month_x = cal.get(Calendar.MONTH);
        day_x = cal.get(Calendar.DAY_OF_MONTH);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        spRole = (Spinner) findViewById(R.id.spnrRole);
        etFirstname = (EditText) findViewById(R.id.etFirstname);
        etMiddlename = (EditText)findViewById(R.id.etMiddlename);
        etLastname = (EditText) findViewById(R.id.etLastname);
        etBirthDate = (EditText) findViewById(R.id.etBirthDate);
        etAddress = (EditText) findViewById(R.id.etUserName);
        etUsername = (EditText) findViewById(R.id.etUserName);
        etPassword = (EditText) findViewById(R.id.etPassword);
        etConfirmPassword = (EditText) findViewById(R.id.etCnfrmPassword);
        etContactnumber = (EditText)findViewById(R.id.etContactnumber);

        etConfirmPassword.setFilters(new InputFilter[]{
                new InputFilter() {
                    @Override
                    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                        if (source.equals("")){
                            return source;
                        }
                        if (source.toString().matches("[a-zA-Z0-9]+")){
                            return source;
                        }
                        return "";
                    }
                }
        });

        etPassword.setFilters(new InputFilter[]{
                new InputFilter() {
                    @Override
                    public CharSequence filter(CharSequence source, int start,
                                               int end, Spanned dest, int dstart, int dend) {

                        if (source.equals("")){
                            return source;
                        }
                        if (source.toString().matches("[a-zA-Z0-9]+")){
                            return source;
                        }
                        return "";
                    }
                }
        });

        btnRegister = (Button) findViewById(R.id.btnRegister);

            btnRegister.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String u = etPassword.getText().toString();
                    String p = etConfirmPassword.getText().toString();
                    if (u.equals(p)) {
                        HashMap postData = new HashMap();

                        postData.put("txtFirstname", etFirstname.getText().toString());
                        postData.put("txtMiddlename", etMiddlename.getText().toString());
                        postData.put("txtLastname", etLastname.getText().toString());
                        postData.put("txtAddress", etAddress.getText().toString());
                        postData.put("txtBirthdate", etBirthDate.getText().toString());
                        postData.put("txtUsername", etUsername.getText().toString());
                        postData.put("txtPassword", etPassword.getText().toString());
                        postData.put("txtRole", spRole.getSelectedItem().toString());
                        postData.put("txtcontactnumber", etContactnumber.getText().toString());

                        if (TextUtils.isEmpty(etFirstname.getText().toString())) {
                            Toast.makeText(RegisterActivity.this, "Insert First Name", Toast.LENGTH_SHORT).show();
                            etPassword.setText("");
                            etConfirmPassword.setText("");
                            return;
                        }
                        if (TextUtils.isEmpty(etMiddlename.getText().toString())) {
                            Toast.makeText(RegisterActivity.this, "Insert Middle Name", Toast.LENGTH_SHORT).show();
                            etPassword.setText("");
                            etConfirmPassword.setText("");
                            return;
                        }
                        if (TextUtils.isEmpty(etLastname.getText().toString())) {
                            Toast.makeText(RegisterActivity.this, "Insert Last Name", Toast.LENGTH_SHORT).show();
                            etPassword.setText("");
                            etConfirmPassword.setText("");
                            return;
                        }
                        if (TextUtils.isEmpty(etAddress.getText().toString())) {
                            Toast.makeText(RegisterActivity.this, "Insert Address", Toast.LENGTH_SHORT).show();
                            etPassword.setText("");
                            etConfirmPassword.setText("");
                            return;
                        }if (TextUtils.isEmpty(etBirthDate.getText().toString())) {
                            Toast.makeText(RegisterActivity.this, "Insert Birthdate", Toast.LENGTH_SHORT).show();
                            etPassword.setText("");
                            etConfirmPassword.setText("");
                            return;
                        }
                        if (TextUtils.isEmpty(etUsername.getText().toString())) {
                            Toast.makeText(RegisterActivity.this, "Insert Username", Toast.LENGTH_SHORT).show();
                            etPassword.setText("");
                            etConfirmPassword.setText("");
                            return;
                        }
                        if (TextUtils.isEmpty(etPassword.getText().toString())) {
                            Toast.makeText(RegisterActivity.this, "Insert Password", Toast.LENGTH_SHORT).show();
                            etPassword.setText("");
                            etConfirmPassword.setText("");
                            return;
                        }
                        if (TextUtils.isEmpty(etConfirmPassword.getText().toString())) {
                            Toast.makeText(RegisterActivity.this, "Insert Password Confirmation", Toast.LENGTH_SHORT).show();
                            etPassword.setText("");
                            etConfirmPassword.setText("");
                            return;
                        }
                        if (TextUtils.isEmpty(etContactnumber.getText().toString())) {
                            Toast.makeText(RegisterActivity.this, "Insert Contact Number", Toast.LENGTH_SHORT).show();
                            etPassword.setText("");
                            etConfirmPassword.setText("");
                            return;
                        }


                        PostResponseAsyncTask taskRegister = new PostResponseAsyncTask(RegisterActivity.this, postData, new AsyncResponse() {
                            @Override
                            public void processFinish(String s) {
                                if (s.contains("New records created successfully")) {
                                    Toast.makeText(RegisterActivity.this, "Registered", Toast.LENGTH_SHORT).show();
                                    Intent in = new Intent(RegisterActivity.this, loginFragment.class);
                                    startActivity(in);
                                    finish();
                                }
                            }
                        });
                        taskRegister.execute("http://carkila.esy.es/carkila/register.php");
                    }else{
                        Toast.makeText(RegisterActivity.this, "Password didn't match", Toast.LENGTH_SHORT).show();

                        etPassword.setText("");
                        etConfirmPassword.setText("");
                    }
                }
            });



        spRole.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    public void showDialogOnButtonClick(){
        etBirthDate = (EditText)findViewById(R.id.etBirthDate);

        etBirthDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(DIALOG_ID);
            }
        });
    }

    @Override
    protected Dialog onCreateDialog(int id) {
      if(id == DIALOG_ID) {
          return new DatePickerDialog(this, dpListener, year_x, month_x, day_x);
      }
          return null;
      }
    DatePickerDialog.OnDateSetListener dpListener
        = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        year_x = year;
        month_x = monthOfYear + 1;
        day_x = dayOfMonth;
        etBirthDate.setText(month_x + "/" +  day_x + "/" + year_x);
    }
    };  
}

php

if(isset($_POST['txtFirstname']) && isset($_POST['txtMiddlename']) && 
isset($_POST['txtLastname']) && isset($_POST['txtAddress']) && 
isset($_POST['txtBirthdate']) && isset($_POST['txtUsername']) &&
isset($_POST['txtPassword']) && isset($_POST['txtRole']) &&
isset($_POST['txtcontactnumber'])){


$firstname = $_POST['txtFirstname'];
$middlename = $_POST['txtMiddlename'];
$lastname = $_POST['txtLastname'];
$address = $_POST['txtAddress'];
$birthdate = $_POST['txtBirthdate'];
$username = $_POST['txtUsername'];
$password = $_POST['txtPassword'];
$roles = $_POST['txtRole'];
$contactnumber = $_POST['txtcontactnumber'];

$stmt = $conn->prepare("INSERT INTO tbl_user (firstname, middlename, lastname, address, birthdate, username, password, roles, contactnumber) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");

$query = "INSERT INTO tbl_user(firstname, middlename, lastname, address, birthdate, username, password, roles, contactnumber) 
VALUES ('$firstname','$middlename','$lastname','$address','$birthdate','$username','$password','$roles','$contactnumber')"; 

$stmt->bind_param("sssssssss", $firstname, $middlename, $lastname, $address, $birthdate, $username, $password, $roles, $contactnumber);
$result = $stmt->execute();

if($result === false ) {
   die('execute() failed: ' . htmlspecialchars($stmt->error));
} else {
   echo "New records created successfully";
}

$stmt->close();
$conn->close();

Thanks guys been stuck here for a long time :)

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • duankangzi766767 duankangzi766767 5年前

    In your PHP code,

    if($result === false ) {
        echo "-1";
    } else {
        echo "1";
    }
    

    In processFinish method,

    @Override
    public void processFinish(String s) {
        if (s.equals("1")) {
            Toast.makeText(RegisterActivity.this, "Registered", Toast.LENGTH_SHORT).show();
            Intent in = new Intent(RegisterActivity.this, loginFragment.class);
            startActivity(in);
            finish();
        } else if (s.equals("-1")) {
            Toast.makeText(RegisterActivity.this, "Your error message", Toast.LENGTH_SHORT).show();     
        }
    }
    
    点赞 评论 复制链接分享