Advertisement
Guest User

yanivJava2

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