Advertisement
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("userNameLogged", 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()==true)
- {
- 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 myPass=myPref.getString(txtUser.getText().toString(), "error");
- if (myPass.equals("error"))
- {
- return false;
- }
- return (txtPass.getText().toString().equals(myPass));
- }
- public void btnReg(View v)
- {
- Intent registerIntent=new Intent(this, Register.class);
- this.startActivity(registerIntent);
- }
- }
- ==============================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();
- if (!password1.equals(password2))
- {
- Toast.makeText(this,"The password must be equls",Toast.LENGTH_LONG).show();
- return;
- }
- String userDB=myPref.getString(userName, "error");
- if (userName.equals(userDB))
- {
- Toast.makeText(this,"The user name exist, please type another user name", Toast.LENGTH_LONG).show();
- return;
- }
- editor.putString(userName,password1);
- editor.commit();
- finish();
- }
- public void btnCancle (View v)
- {
- Intent myLog=new Intent(this,MainActivity.class);
- this.startActivity(myLog);
- }
- }
- =================================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.helloUser);
- myTxt.setText("Hello" + getIntent().getStringExtra("userNameLogged"));
- // myTxt.setText("Hello "+getUserName());
- }
- /*private String getUserName()
- {
- SharedPreferences myPref=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
- userName=myPref.getString("logged","error");
- return userName;
- }*/
- public void btnLogout (View v)
- {
- SharedPreferences myPref=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
- SharedPreferences.Editor editor=myPref.edit();
- editor.remove("logged");
- editor.commit();
- Intent mainIntent=new Intent(this,MainActivity.class);
- this.startActivity(mainIntent);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement