Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Expecificação
- EditText editEmail, editSenha; //componetntes do projeto
- SQLiteDatabase db; //DB é o banco de dados
- //SharedPreferences
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_cadastro);
- if (getIntent().getBooleanExtra("Exit", false)){
- finish();
- }
- editEmail=(EditText)findViewById(R.id.editEmail);
- editSenha=(EditText)findViewById(R.id.editSenha);
- db=openOrCreateDatabase("CadastroDB", Context.MODE_PRIVATE, null);
- db.execSQL("CREATE TABLE IF NOT EXISTS cadastro (Email VARCHAR, Senha VARCHAR);");
- }
- //Botão Cadastrar
- public void startDesligar (View view) {
- if(isEmailValid(editEmail.getText().toString().trim()) && isPasswordValid(editSenha.getText().toString().trim()))
- {
- db.execSQL("INSERT INTO cadastro VALUES('" + editEmail.getText() + "','" + editSenha.getText() + "');");
- showMessage("Tudo certo!", "Dados Gravados");
- clearText();
- Toast.makeText(getApplicationContext(), "Redirecionando...", Toast.LENGTH_LONG).show();
- Intent Desligar = new Intent(this, Desligar.class);
- startActivity(Desligar);
- } else {
- showMessage("Preencha os campos corretamente!", "E-mail/Senha inválido");
- }
- }
- //ShowMessage
- public void showMessage(String title, String message)
- {
- AlertDialog.Builder builder=new AlertDialog.Builder(this);
- builder.setCancelable(true);
- builder.setTitle(title);
- builder.setMessage(message);
- builder.show();
- }
- //ClearText
- public void clearText()
- {
- editEmail.setText("");
- editSenha.setText("");
- }
- private boolean isEmailValid (String email) {
- Pattern pattern;
- Matcher matcher;
- final String EMAIL_PATTERN = (".+@.+\.[a-z]+");
- pattern = Pattern.compile(EMAIL_PATTERN);
- matcher = pattern.matcher(email);
- return matcher.matches();
- }
- private boolean isPasswordValid (String password) {
- Pattern pattern;
- Matcher matcher;
- final String PASSWORD_PATTERN = "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=\S+$).{4,}$";
- pattern = Pattern.compile(PASSWORD_PATTERN);
- matcher = pattern.matcher(password);
- return matcher.matches();
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_desligar); // Determina qual layout vai abrir
- senha = (EditText)findViewById(R.id.editSenha); // Senha = EditSenha
- tentativas = (EditText)findViewById(R.id.editTentativas); // Tentativa = editTentativas
- btnir = (Button)findViewById(R.id.button);
- }
- public void startRecuperacao (View view) {
- Intent Recuperacao = new Intent(this, Recuperacao.class);
- startActivity(Recuperacao);
- }
- public void btndesligar (View view) {
- if
- (senha.getText().toString().equals("senha digitada")) {
- Intent intent = new Intent(this, CadastroActivity.class);
- intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- intent.putExtra("Exit", true);
- Toast.makeText(getApplicationContext(), "Desligando...", Toast.LENGTH_SHORT).show();
- startActivity(intent);
- finish();
- } else {
- Toast.makeText(getApplicationContext(), "Senha Incorreta", Toast.LENGTH_SHORT).show();
- tentativas.setVisibility(View.VISIBLE); //Deixar o texto com o número de tentativas
- counter--;
- tentativas.setText(Integer.toString(counter));
- if (counter == 0) {
- btnir.setEnabled(false); //Desabilitar o botão de Desligar
- }
- }
- }
- public void btndesligar (View view) {
- String query = "SELECT * FROM cadastro";
- Cursor cursor = db.rawQuery(query, null);
- if(cursor.moveToFirst()){
- do{
- if(senha.getText().toString().equals(cursor.getString(1))){
- Intent intent = new Intent(this, CadastroActivity.class);
- intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- intent.putExtra("Exit", true);
- Toast.makeText(getApplicationContext(), "Desligando...", Toast.LENGTH_SHORT).show();
- startActivity(intent);
- finish();
- }
- } while(cursor.moveToNext());
- Toast.makeText(getApplicationContext(), "Senha Incorreta", Toast.LENGTH_SHORT).show();
- tentativas.setVisibility(View.VISIBLE); //Deixar o texto com o número de tentativas
- counter--;
- tentativas.setText(Integer.toString(counter));
- if (counter == 0) {
- btnir.setEnabled(false); //Desabilitar o botão de Desligar
- }
- }
Add Comment
Please, Sign In to add comment