Advertisement
Guest User

yanivJava1

a guest
Mar 11th, 2016
106
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("userNameLogged", 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("userName", "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("userNameLogged", "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. /==========================Register
  70. package com.example.y.login;
  71.  
  72. import android.app.Activity;
  73. import android.content.Intent;
  74. import android.content.SharedPreferences;
  75. import android.os.Bundle;
  76. import android.view.View;
  77. import android.widget.EditText;
  78. import android.widget.Toast;
  79.  
  80. public class Register extends Activity
  81. {
  82. EditText txtUser;
  83. EditText txtPass1;
  84. EditText txtPass2;
  85.  
  86. @Override
  87. protected void onCreate(Bundle savedInstanceState)
  88. {
  89. super.onCreate(savedInstanceState);
  90. setContentView(R.layout.regis);
  91. setPointer();
  92. }
  93. public void setPointer()
  94. {
  95. txtUser=(EditText)findViewById(R.id.user);
  96. txtPass1=(EditText)findViewById(R.id.passReg1);
  97. txtPass2=(EditText)findViewById(R.id.passReg2);
  98. }
  99. public void btnRegister (View v)
  100. {
  101. SharedPreferences myPref=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
  102. SharedPreferences.Editor editor=myPref.edit();
  103. String userName=txtUser.getText().toString();
  104. String password1=txtPass1.getText().toString();
  105. String password2=txtPass2.getText().toString();
  106. String userDB=myPref.getString("userName","error");
  107. if (userName.equals(userDB))
  108. {
  109. Toast.makeText(this,"The user name exist, please type another user name", Toast.LENGTH_LONG).show();
  110. return;
  111. }
  112. else if (!password1.equals(password2))
  113. {
  114. Toast.makeText(this,"The password must be equls",Toast.LENGTH_LONG).show();
  115. return;
  116. }
  117. editor.putString("userNameLogged", userName);
  118. editor.putString("userNameLogged", password1);
  119. editor.commit();
  120. Intent myLogin=new Intent(this, MainActivity.class);
  121. this.startActivity(myLogin);
  122. }
  123. public void btnCancle (View v)
  124. {
  125. Intent myLog=new Intent(this,MainActivity.class);
  126. this.startActivity(myLog);
  127. }
  128. }
  129. //====================================Hello
  130. package com.example.y.login;
  131.  
  132.  
  133. import android.app.Activity;
  134. import android.content.Intent;
  135. import android.content.SharedPreferences;
  136. import android.os.Bundle;
  137. import android.view.View;
  138. import android.widget.TextView;
  139.  
  140. public class Hello extends Activity
  141. {
  142. String userName;
  143. TextView myTxt;
  144. @Override
  145. protected void onCreate(Bundle savedInstanceState)
  146. {
  147. super.onCreate(savedInstanceState);
  148. setContentView(R.layout.hello);
  149. myTxt=(TextView)findViewById(R.id.user);
  150. myTxt.setText("Hello"+getUserName());
  151. }
  152.  
  153. private String getUserName()
  154. {
  155. SharedPreferences myPref=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
  156. userName=myPref.getString("userNameLogged","error");
  157. return userName;
  158. }
  159.  
  160. public void btnLogout (View v)
  161. {
  162. SharedPreferences myPref=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
  163. SharedPreferences.Editor editor=myPref.edit();
  164. editor.remove("userNameLogged");
  165. editor.commit();
  166. Intent mainIntent=new Intent(this,MainActivity.class);
  167. this.startActivity(mainIntent);
  168. }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement