Guest User

yanivJava

a guest
Mar 11th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. //============================MainActivity
  2. package com.example.y.login;
  3.  
  4. import android.app.Activity;
  5. import android.content.Intent;
  6. import android.content.SharedPreferences;
  7. import android.os.Bundle;
  8. import android.view.Menu;
  9. import android.view.MenuItem;
  10. import android.view.View;
  11. import android.widget.EditText;
  12. import android.widget.Toast;
  13.  
  14. public class MainActivity extends Activity
  15. {
  16. EditText txtUser;
  17. EditText txtPass;
  18. String userNameLogged;
  19.  
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState)
  22. {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_main);
  25. txtUser = (EditText) findViewById(R.id.user);
  26. txtPass = (EditText) findViewById(R.id.pass);
  27. if (userLogged())
  28. {
  29. Intent myLogged = new Intent(this, Hello.class);
  30. myLogged.putExtra("userName", userNameLogged);
  31. this.startActivity(myLogged);
  32. }
  33. }
  34.  
  35. private boolean userLogged()
  36. {
  37. SharedPreferences myPref = getApplication().getSharedPreferences("myPref", MODE_PRIVATE);
  38. userNameLogged = myPref.getString("logged", "error");
  39. if (userNameLogged.equals("error"))
  40. return false;
  41. else
  42. return true;
  43. }
  44.  
  45. public void btnLogin(View v)
  46. {
  47. if (checkUser())
  48. {
  49. Intent helloIntent = new Intent(this, Hello.class);
  50. this.startActivity(helloIntent);
  51. } else
  52. Toast.makeText(this, "Wrong user name or password!", Toast.LENGTH_LONG).show();
  53. }
  54. private boolean checkUser()
  55. {
  56. SharedPreferences myPref=getApplication().getSharedPreferences("myPref",MODE_PRIVATE);
  57. String userName=myPref.getString("userName", "error");
  58. String userPass=myPref.getString("userPass","error");
  59. if (userName.equals(txtUser.getText().toString())&&userPass.equals(txtPass.getText().toString()))
  60. return true;
  61. else
  62. return false;
  63. }
  64. public void btnReg(View v)
  65. {
  66. Intent registerIntent=new Intent(this, Register.class);
  67. this.startActivity(registerIntent); }
  68. }
  69. //=============================================Hello
  70. package com.example.y.login;
  71.  
  72.  
  73. import android.app.Activity;
  74. import android.content.Intent;
  75. import android.content.SharedPreferences;
  76. import android.os.Bundle;
  77. import android.view.View;
  78. import android.widget.TextView;
  79.  
  80. public class Hello extends Activity
  81. {
  82. String userName;
  83. TextView myTxt;
  84. @Override
  85. protected void onCreate(Bundle savedInstanceState)
  86. {
  87. super.onCreate(savedInstanceState);
  88. setContentView(R.layout.hello);
  89. myTxt=(TextView)findViewById(R.id.user);
  90. myTxt.setText("Hello"+getUserName());
  91. }
  92.  
  93. private String getUserName()
  94. {
  95. SharedPreferences myPref=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
  96. userName=myPref.getString("user","error");
  97. return userName;
  98. }
  99.  
  100. public void btnLogout (View v)
  101. {
  102. SharedPreferences myPref=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
  103. SharedPreferences.Editor editor=myPref.edit();
  104. editor.remove("user");
  105. editor.commit();
  106. Intent mainIntent=new Intent(this,MainActivity.class);
  107. this.startActivity(mainIntent);
  108. }
  109. }
  110. /=============================================Register
  111. package com.example.y.login;
  112.  
  113. import android.app.Activity;
  114. import android.content.Intent;
  115. import android.content.SharedPreferences;
  116. import android.os.Bundle;
  117. import android.view.View;
  118. import android.widget.EditText;
  119. import android.widget.Toast;
  120.  
  121. public class Register extends Activity
  122. {
  123. EditText txtUser;
  124. EditText txtPass1;
  125. EditText txtPass2;
  126.  
  127. @Override
  128. protected void onCreate(Bundle savedInstanceState)
  129. {
  130. super.onCreate(savedInstanceState);
  131. setContentView(R.layout.regis);
  132. setPointer();
  133. }
  134. public void setPointer()
  135. {
  136. txtUser=(EditText)findViewById(R.id.user);
  137. txtPass1=(EditText)findViewById(R.id.passReg1);
  138. txtPass2=(EditText)findViewById(R.id.passReg2);
  139. }
  140. public void btnRegister (View v)
  141. {
  142. SharedPreferences myPref=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
  143. SharedPreferences.Editor editor=myPref.edit();
  144. String userName=txtUser.getText().toString();
  145. String password1=txtPass1.getText().toString();
  146. String password2=txtPass2.getText().toString();
  147. String userDB=myPref.getString("userNameLogged","error");
  148. if (userName.equals(userDB))
  149. {
  150. Toast.makeText(this,"The user name exist, please type another user name", Toast.LENGTH_LONG).show();
  151. return;
  152. }
  153. else if (!password1.equals(password2))
  154. {
  155. Toast.makeText(this,"The password must be equls",Toast.LENGTH_LONG).show();
  156. return;
  157. }
  158. editor.putString("userNameLogged", userName);
  159. editor.putString("userNameLogged", password1);
  160. editor.commit();
  161. Intent myLogin=new Intent(this, MainActivity.class);
  162. this.startActivity(myLogin);
  163. }
  164. public void btnCancle (View v)
  165. {
  166. Intent myLog=new Intent(this,MainActivity.class);
  167. this.startActivity(myLog);
  168. }
  169. }
Add Comment
Please, Sign In to add comment