Advertisement
Guest User

yanivJava3

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