Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.6 kB
2
Indexable
Never
public class LoginActivity extends AppCompatActivity {
    private EditText etxtPassword;
    private TextView txtHello, txtErrorPass;

    private Button btnLogin;
    private FirebaseDatabase database;
    private DatabaseReference myRef;
    private Intent intent;
    String nameStudio = null, pass;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        init();
        connectFB();
    }
    private void init(){
        etxtPassword = findViewById(R.id.etxtPassword);
        txtHello = findViewById(R.id.txtHello);
        txtErrorPass = findViewById(R.id.txtErrorPassword);
        btnLogin = findViewById(R.id.btnLogin);
    }

    private void connectFB(){
        FirebaseApp.initializeApp(this);
        database = FirebaseDatabase.getInstance();
        myRef = database.getReference("Name_Studio");
    }

    @Override
    protected void onResume() {
        super.onResume();
        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                nameStudio = snapshot.getValue(String.class);
                if(nameStudio == null){
                    intent = new Intent(getApplicationContext(), NameStudioActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
            @Override
            public void onCancelled(@NonNull DatabaseError error) {
            }
        });

        utisLogin();
    }

    private void utisLogin(){
        txtHello.setText("Hello " + nameStudio);
        pass = etxtPassword.getText().toString().trim();
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(pass.equals("123")){
                    intent = new Intent(getApplicationContext(), MainActivity.class);
                    startActivity(intent);
                    finish();
                }else{
                    txtErrorPass.setVisibility(View.VISIBLE);
                    txtErrorPass.setText("Sai mật khẩu. Vui lòng thử lại.");
                }
            }
        });
        txtErrorPass.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                txtErrorPass.setVisibility(View.GONE);
                return false;
            }
        });
    }
}