Advertisement
Guest User

forIgor

a guest
Mar 16th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.33 KB | None | 0 0
  1. =====Main
  2. package com.example.shiv2.mytest;
  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.View;
  9. import android.widget.EditText;
  10. import android.widget.Toast;
  11.  
  12. public class MainActivity extends Activity
  13. {
  14. SharedPreferences myPref;
  15. SharedPreferences.Editor editor;
  16.  
  17. private EditText userName;
  18. private EditText passWord;
  19.  
  20. private String isOnline;
  21.  
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState)
  24. {
  25. super.onCreate(savedInstanceState);
  26. setContentView(R.layout.activity_main);
  27. setPoints();
  28. if(chkOnline())
  29. {
  30. Intent userLogged = new Intent(this,Logged.class);
  31. userLogged.putExtra("userOnline",isOnline);
  32. this.startActivity(userLogged);
  33. }
  34. }
  35.  
  36. private void setPoints()
  37. {
  38. myPref = getApplicationContext().getSharedPreferences(Register.myPrefs,MODE_PRIVATE);
  39. editor =myPref.edit();
  40.  
  41. userName = (EditText)findViewById(R.id.inUser);
  42. passWord = (EditText)findViewById(R.id.inPass);
  43. }
  44.  
  45. private boolean chkOnline()
  46. {
  47. isOnline = myPref.getString("userOnline","offline");
  48.  
  49. return !isOnline.equals("offline");
  50.  
  51. }
  52.  
  53. private void chkUser()
  54. {
  55. String userInput = userName.getText().toString();
  56. String passInput = passWord.getText().toString();
  57.  
  58. if(userInput.trim().equals("") || passInput.trim().equals(""))
  59. {
  60. Toast.makeText(this,"Error, please fill all the fields",Toast.LENGTH_SHORT).show();
  61. }
  62. else if(!myPref.contains(userInput) || !passInput.equals(myPref.getString(userInput,passInput)))
  63. {
  64. Toast.makeText(this,"Error, wrong username or password!",Toast.LENGTH_SHORT).show();
  65. }
  66. else
  67. {
  68. Toast.makeText(this,"Success, logged in successfully!",Toast.LENGTH_SHORT).show();
  69.  
  70. editor.putString("userOnline",userInput);
  71. editor.commit();
  72.  
  73. Intent logMe = new Intent(this,Logged.class);
  74. this.startActivity(logMe);
  75. }
  76. }
  77.  
  78. public void btnLog(View v)
  79. {
  80. chkUser();
  81. }
  82. public void btnReg(View v)
  83. {
  84. Intent regInt = new Intent(this,Register.class);
  85. this.startActivity(regInt);
  86. }
  87.  
  88. }
  89.  
  90. ======
  91.  
  92. ====register
  93. package com.example.shiv2.mytest;
  94.  
  95. import android.app.Activity;
  96. import android.content.Intent;
  97. import android.content.SharedPreferences;
  98. import android.os.Bundle;
  99. import android.view.View;
  100. import android.widget.EditText;
  101. import android.widget.Toast;
  102.  
  103. /**
  104. * Created by app0811 on 16/03/2016.
  105. */
  106. public class Register extends Activity
  107. {
  108. public final static String myPrefs = "myPref";//shared prefrences xml file name
  109.  
  110. SharedPreferences myPref;
  111. SharedPreferences.Editor editor;
  112.  
  113. private EditText userInput;
  114. private EditText passInput;
  115. private EditText rePassInput;
  116.  
  117. @Override
  118. protected void onCreate(Bundle savedInstanceState)
  119. {
  120. super.onCreate(savedInstanceState);
  121. setContentView(R.layout.activity_register);
  122. setPoints();
  123. }
  124.  
  125. private void setPoints()
  126. {
  127. myPref = getApplicationContext().getSharedPreferences(myPrefs,MODE_PRIVATE);
  128. editor =myPref.edit();
  129.  
  130. userInput = (EditText)findViewById(R.id.setUser);
  131. passInput = (EditText)findViewById(R.id.setPass);
  132. rePassInput = (EditText)findViewById(R.id.confPass);
  133. }
  134.  
  135. private void chkReg()
  136. {
  137. String userName = userInput.getText().toString();
  138. String passWord = passInput.getText().toString();
  139. String checkPass = rePassInput.getText().toString();
  140.  
  141. if(userName.trim().length()<=0 || passWord.trim().length()<=0 || checkPass.trim().length()<=0)
  142. {
  143. Toast.makeText(this, "Error, please fill all the fields", Toast.LENGTH_SHORT).show();
  144. }
  145. else if(!checkPass.equals(passWord))
  146. {
  147. Toast.makeText(this, "Error, Password do not match!", Toast.LENGTH_SHORT).show();
  148. }
  149. else if(myPref.contains(userName))
  150. {
  151. Toast.makeText(this, "Error, this user allready exists", Toast.LENGTH_SHORT).show();
  152. }
  153. else
  154. {
  155. Toast.makeText(this, "Success, registered succesfully", Toast.LENGTH_SHORT).show();
  156. editor.putString(userName, passWord);
  157. editor.commit();
  158.  
  159. Intent mainInt = new Intent(this,MainActivity.class);
  160. this.startActivity(mainInt);
  161. finish();
  162. }
  163. }
  164.  
  165. public void btnReg(View v)
  166. {
  167. chkReg();
  168. }
  169.  
  170. public void btnCncle(View v)
  171. {
  172. Intent cnclInten = new Intent (this,MainActivity.class);
  173. this.startActivity(cnclInten);
  174. finish();
  175. }
  176. }
  177. =========
  178.  
  179. ====logged
  180. package com.example.shiv2.mytest;
  181.  
  182. import android.app.Activity;
  183. import android.content.Context;
  184. import android.content.Intent;
  185. import android.content.SharedPreferences;
  186. import android.os.Bundle;
  187. import android.view.View;
  188. import android.widget.TextView;
  189.  
  190. /**
  191. * Created by app0811 on 16/03/2016.
  192. */
  193. public class Logged extends Activity
  194. {
  195. SharedPreferences myPrefs; //calling SharedPrefrences Class
  196. SharedPreferences.Editor editor;//calling the editor for SharedPrefrences
  197.  
  198. TextView userLogged; //what kind of user is logged in
  199.  
  200. @Override
  201. protected void onCreate(Bundle savedInstanceState)
  202. {
  203. super.onCreate(savedInstanceState);
  204. setContentView(R.layout.activity_logged);
  205. setPoints();
  206. userLogged.setText("Hello " + getUserName());
  207. }
  208.  
  209. private void setPoints()
  210. {
  211. userLogged = (TextView)findViewById(R.id.logMsg);
  212. myPrefs = getApplicationContext().getSharedPreferences(Register.myPrefs,MODE_PRIVATE);
  213. editor = myPrefs.edit();
  214. }
  215.  
  216. private String getUserName()
  217. {
  218. String username = myPrefs.getString("userOnline","offline");
  219. return username;
  220. }
  221.  
  222. public void btnLogOut(View v)
  223. {
  224. editor.remove("userOnline");
  225. editor.commit();
  226.  
  227. Intent logOut = new Intent(this,MainActivity.class);
  228. this.startActivity(logOut);
  229. }
  230.  
  231.  
  232.  
  233. }
  234. ======
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement