Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.shalty.login;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- public class Reg extends AppCompatActivity {
- EditText userName;
- EditText password;
- EditText verPassword;
- SharedPreferences prefs;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_reg);
- userName = (EditText)findViewById(R.id.userNameReg);
- password = (EditText)findViewById(R.id.passReg);
- verPassword = (EditText)findViewById(R.id.passVal);
- prefs=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
- }
- public void SignUp(View v)
- {
- if(password.getText().toString().equals(verPassword.getText().toString()))
- {
- if(prefs.getString(userName.getText().toString(), "notExist").equals("notExist")) {
- saveData();
- Intent logIn = new Intent(this, Login.class);
- this.startActivity(logIn);
- }
- else
- {
- Toast.makeText(this, "Username already exists Please choose another ", Toast.LENGTH_LONG).show();
- this.startActivity(new Intent(this,Reg.class));
- }
- }else {
- Toast.makeText(this, "Passwords do not match, please try again", Toast.LENGTH_LONG).show();
- this.startActivity(new Intent(this,Reg.class));
- }
- }
- private void saveData()
- {
- SharedPreferences.Editor editor=prefs.edit();
- editor.putString(userName.getText().toString(), password.getText().toString());
- editor.commit();
- // Toast.makeText(this, "============saveData==============", Toast.LENGTH_LONG).show();
- }
- public void Cancel(View v)
- {
- Intent login = new Intent(this, Login.class);
- this.startActivity(login);
- }
- }
- =====================================================================================
- package com.example.shalty.login;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- public class Login extends AppCompatActivity {
- private EditText userName;
- private EditText password;
- SharedPreferences prefs;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- prefs=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
- userName =(EditText)findViewById(R.id.userName);
- password =(EditText)findViewById(R.id.pass);
- if(prefs.getString(userName.getText().toString(),"logdIn").toString().equals("logdIn"))
- {
- Intent HomePage = new Intent(this,HomePage.class);
- HomePage.putExtra("Name",prefs.getString(userName.getText().toString(), "logdIn"));
- }
- }
- public void signUp(View v)
- {
- Intent signUp = new Intent(this,Reg.class);
- this.startActivity(signUp);
- }
- public void logIn(View v)
- {
- if (!prefs.getString(userName.getText().toString(), "notExist").toString().equals("notExist"))
- {
- if (!prefs.getString(password.getText().toString(), "notExist").toString().equals("notExist"))
- {
- Intent homPage = new Intent(this, HomePage.class);
- homPage.putExtra("name",userName.getText().toString());;
- SharedPreferences.Editor editor=prefs.edit();
- editor.putString(userName.getText().toString(), "logdIn");
- startActivity(homPage);
- } else
- Toast.makeText(this, "The username or password are incorrect", Toast.LENGTH_LONG).show();
- }
- else
- Toast.makeText(this, "Username does not exist please signup ", Toast.LENGTH_LONG).show();
- }
- }
- ===================================================================================================
- package com.example.shalty.login;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.RelativeLayout;
- import android.widget.TextView;
- public class HomePage extends AppCompatActivity {
- TextView hom ;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_home_page);
- hom = (TextView)findViewById(R.id.helow);
- hom.setText("Hello " + getIntent().getStringExtra("name"));
- }
- public void logUot(View v)
- {
- SharedPreferences prefs=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
- SharedPreferences.Editor editor=prefs.edit();
- editor.remove("logdIn");
- editor.commit();
- Intent login = new Intent(this,Login.class);
- this.startActivity(login);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement