Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //============================MainActivity
- package com.example.y.login;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
- public class MainActivity extends Activity
- {
- EditText txtUser;
- EditText txtPass;
- String userNameLogged;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- txtUser = (EditText) findViewById(R.id.user);
- txtPass = (EditText) findViewById(R.id.pass);
- if (userLogged())
- {
- Intent myLogged = new Intent(this, Hello.class);
- myLogged.putExtra("userName", userNameLogged);
- this.startActivity(myLogged);
- }
- }
- private boolean userLogged()
- {
- SharedPreferences myPref = getApplication().getSharedPreferences("myPref", MODE_PRIVATE);
- userNameLogged = myPref.getString("logged", "error");
- if (userNameLogged.equals("error"))
- return false;
- else
- return true;
- }
- public void btnLogin(View v)
- {
- if (checkUser())
- {
- Intent helloIntent = new Intent(this, Hello.class);
- this.startActivity(helloIntent);
- } else
- Toast.makeText(this, "Wrong user name or password!", Toast.LENGTH_LONG).show();
- }
- private boolean checkUser()
- {
- SharedPreferences myPref=getApplication().getSharedPreferences("myPref",MODE_PRIVATE);
- String userName=myPref.getString("userName", "error");
- String userPass=myPref.getString("userPass","error");
- if (userName.equals(txtUser.getText().toString())&&userPass.equals(txtPass.getText().toString()))
- return true;
- else
- return false;
- }
- public void btnReg(View v)
- {
- Intent registerIntent=new Intent(this, Register.class);
- this.startActivity(registerIntent); }
- }
- //=============================================Hello
- package com.example.y.login;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.TextView;
- public class Hello extends Activity
- {
- String userName;
- TextView myTxt;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.hello);
- myTxt=(TextView)findViewById(R.id.user);
- myTxt.setText("Hello"+getUserName());
- }
- private String getUserName()
- {
- SharedPreferences myPref=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
- userName=myPref.getString("user","error");
- return userName;
- }
- public void btnLogout (View v)
- {
- SharedPreferences myPref=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
- SharedPreferences.Editor editor=myPref.edit();
- editor.remove("user");
- editor.commit();
- Intent mainIntent=new Intent(this,MainActivity.class);
- this.startActivity(mainIntent);
- }
- }
- /=============================================Register
- package com.example.y.login;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
- public class Register extends Activity
- {
- EditText txtUser;
- EditText txtPass1;
- EditText txtPass2;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.regis);
- setPointer();
- }
- public void setPointer()
- {
- txtUser=(EditText)findViewById(R.id.user);
- txtPass1=(EditText)findViewById(R.id.passReg1);
- txtPass2=(EditText)findViewById(R.id.passReg2);
- }
- public void btnRegister (View v)
- {
- SharedPreferences myPref=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
- SharedPreferences.Editor editor=myPref.edit();
- String userName=txtUser.getText().toString();
- String password1=txtPass1.getText().toString();
- String password2=txtPass2.getText().toString();
- String userDB=myPref.getString("userNameLogged","error");
- if (userName.equals(userDB))
- {
- Toast.makeText(this,"The user name exist, please type another user name", Toast.LENGTH_LONG).show();
- return;
- }
- else if (!password1.equals(password2))
- {
- Toast.makeText(this,"The password must be equls",Toast.LENGTH_LONG).show();
- return;
- }
- editor.putString("userNameLogged", userName);
- editor.putString("userNameLogged", password1);
- editor.commit();
- Intent myLogin=new Intent(this, MainActivity.class);
- this.startActivity(myLogin);
- }
- public void btnCancle (View v)
- {
- Intent myLog=new Intent(this,MainActivity.class);
- this.startActivity(myLog);
- }
- }
Add Comment
Please, Sign In to add comment