Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.android.mybbideal;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.widget.Button;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- public class AuthActivity extends AppCompatActivity {
- SharedPreferences sharedPreferences;
- String userName;
- String userPwd;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- sharedPreferences = getSharedPreferences(Constant.MyPREFERENCES,Context.MODE_PRIVATE);
- userName = sharedPreferences.getString(Constant.Name,"0");
- setContentView(R.layout.activity_auth);
- Button buttonAuth = (Button) this.findViewById(R.id.button_auth);
- if(userName.equals("0")) {
- buttonAuth.setText("Daftar");
- } else {
- buttonAuth.setText("Login");
- }
- TextView txtLupaPwd = (TextView) this.findViewById(R.id.text_lupa_pwd_login);
- txtLupaPwd.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- AuthActivity.this.finish();
- Intent intent = new Intent(AuthActivity.this, ForgotPwdActivity.class);
- AuthActivity.this.startActivity(intent);
- }
- });
- EditText editUsername = (EditText) findViewById(R.id.username);
- EditText editPassword = (EditText) findViewById(R.id.password);
- if (savedInstanceState == null) {
- Bundle extras = getIntent().getExtras();
- if(extras == null) {
- // do nothing
- } else {
- editUsername.setText( extras.getString("extraUser"));
- editPassword.setText( extras.getString("extraPwd") );
- }
- }
- }
- public void prosesAuth(View view) {
- EditText editUsername = (EditText) findViewById(R.id.username);
- EditText editPassword = (EditText) findViewById(R.id.password);
- String u = editUsername.getText().toString();
- String p = editPassword.getText().toString();
- Button btnBtn = (Button) this.findViewById(R.id.button_auth);
- if( btnBtn.getText().toString().equals("Daftar") ) {
- SharedPreferences.Editor editor = sharedPreferences.edit();
- editor.putString(Constant.Name, u);
- editor.putString(Constant.Pwd, p);
- editor.apply();
- goToActivity(MainActivity.class);
- } else {
- userName = sharedPreferences.getString(Constant.Name,"0");
- userPwd = sharedPreferences.getString(Constant.Pwd,"0");
- if( u.equals(userName) && p.equals(userPwd) ) {
- goToActivity(MainActivity.class);
- } else {
- Toast.makeText(this, "Invalid username/password!", Toast.LENGTH_SHORT).show();
- }
- }
- }
- public void goToActivity(Class act) {
- this.finish();
- Intent intent = new Intent(this,act);
- intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- this.startActivity(intent);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement