Advertisement
yuli_mordek

HomeWork Registration

Mar 12th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.14 KB | None | 0 0
  1. Main Activity Java///////////////////////////////
  2.  
  3. package com.mordekicloud.yuli.user123;
  4.  
  5. import android.app.Activity;
  6. import android.content.Intent;
  7. import android.content.SharedPreferences;
  8. import android.os.Bundle;
  9. import android.support.v7.app.ActionBarActivity;
  10. import android.view.Menu;
  11. import android.view.MenuItem;
  12. import android.view.View;
  13. import android.widget.EditText;
  14. import android.widget.Toast;
  15.  
  16. public class MainActivity extends Activity {
  17.  
  18.  
  19.     EditText txtUserName;
  20.     EditText txtPassword;
  21.  
  22.     @Override
  23.     protected void onCreate(Bundle savedInstanceState) {
  24.         super.onCreate(savedInstanceState);
  25.         setContentView(R.layout.activity_main);
  26.  
  27.         txtUserName = (EditText) findViewById(R.id.txtUser1);
  28.         txtPassword = (EditText) findViewById(R.id.txtPass1);
  29.  
  30.         if (CheckUser()) {
  31.             SharedPreferences
  32.                     pref = getApplicationContext().getSharedPreferences("Pref", MODE_PRIVATE);
  33.             Intent loggedIntent = new Intent(this, Daf3.class);
  34.             loggedIntent.putExtra("userPut", pref.getString("Logged In", "Error"));
  35.  
  36.  
  37.             this.startActivity(loggedIntent);
  38.         }
  39.     }
  40.  
  41.  
  42.         private boolean CheckUser ()
  43.         {
  44.             SharedPreferences
  45.                     pref = getApplicationContext().getSharedPreferences("user", MODE_PRIVATE);
  46.             String userName = pref.getString("Logged In", "Error");
  47.             return (!userName.equals("Error"));
  48.         }
  49.  
  50.     public void  btnLogIn(View v) {
  51.         SharedPreferences pref =
  52.                 getApplicationContext().getSharedPreferences("user", MODE_PRIVATE);
  53.         String logUserName = txtUserName.getText().toString().toLowerCase();
  54.         String logPassword = txtPassword.getText().toString();
  55.         String logPasswordConf = pref.getString(logUserName, "Error");
  56.  
  57.         if (logUserName.isEmpty() || logPassword.isEmpty()) {
  58.             Toast.makeText(this, "Ther is one or more fields empty", Toast.LENGTH_LONG).show();
  59.             return;
  60.         }
  61.         else if (!pref.contains(logUserName) || !logPassword.equals(logPasswordConf)) {
  62.                 Toast.makeText(this, "Invald user name or password", Toast.LENGTH_LONG).show();
  63.                 return;
  64.             }
  65.          else {
  66.             SharedPreferences.Editor editor = pref.edit();
  67.             editor.putString("Logged", logUserName).commit();
  68.  
  69.             Toast.makeText(this, "You logged successfuly", Toast.LENGTH_SHORT).show();
  70.             Intent loggedIntent = new Intent(this, Daf3.class);
  71.             loggedIntent.putExtra("userPut", pref.getString("Logged", "Error"));
  72.             this.startActivity(loggedIntent);
  73.         }
  74.         }
  75.  
  76.  
  77.  
  78.             public void btnSingUp (View v)
  79.             {
  80.                 Intent singIntent = new Intent(this, Daf2.class);
  81.                 this.startActivity(singIntent);
  82.             }
  83.  
  84.  
  85.  
  86.         }
  87.  
  88.  
  89. Daf2 java//////////////////////////////////////
  90.  
  91. package com.mordekicloud.yuli.user123;
  92.  
  93. import android.app.Activity;
  94. import android.content.Intent;
  95. import android.content.SharedPreferences;
  96. import android.os.Bundle;
  97. import android.view.View;
  98. import android.widget.EditText;
  99. import android.widget.Toast;
  100.  
  101. import java.util.IllegalFormatCodePointException;
  102.  
  103. /**
  104.  * Created by yuli.mordek on 3/11/2016.
  105.  */
  106. public class Daf2 extends Activity
  107. {
  108.     EditText txtUserName;
  109.     EditText txtPassword;
  110.     EditText txtComfPass;
  111.  
  112.     @Override
  113.     protected void onCreate(Bundle savedInstanceState) {
  114.         super.onCreate(savedInstanceState);
  115.         setContentView(R.layout.daf2);
  116.         txtUserName=(EditText)findViewById(R.id.UserName);
  117.         txtPassword=(EditText)findViewById(R.id.Password);
  118.         txtComfPass=(EditText)findViewById(R.id.ConfPass);
  119.  
  120.     }
  121.  
  122.     public void btnCancel (View v)
  123.     {
  124.         Intent cancelIntent=new Intent(this, MainActivity.class);
  125.         this.startActivity(cancelIntent);
  126.     }
  127.  
  128.     public void btnEnter(View v)
  129.     {
  130.         SharedPreferences
  131.                 pref=getApplicationContext().getSharedPreferences("user",MODE_PRIVATE);
  132.         SharedPreferences.Editor editor=pref.edit();
  133.  
  134.         String  UserName=txtUserName.getText().toString().toLowerCase();
  135.         String  Password=txtPassword.getText().toString().toLowerCase();
  136.         String  ConfPass=txtComfPass.getText().toString().toLowerCase();
  137.  
  138.         if (UserName.isEmpty() || Password.isEmpty() || ConfPass.isEmpty())
  139.         {
  140.             Toast.makeText(this,"You must fill all fields",Toast.LENGTH_LONG).show();
  141.             return;
  142.         }
  143.         else if(pref.contains(UserName))
  144.         {
  145.             Toast.makeText(this,"User you exists",Toast.LENGTH_SHORT).show();
  146.             return;
  147.         }
  148.  
  149.         else if (!Password.equals(ConfPass))
  150.         {
  151.             Toast.makeText(this, "Password is not correct", Toast.LENGTH_LONG).show();
  152.             return;
  153.         }
  154.         Toast.makeText(this, "You registreted successful", Toast.LENGTH_SHORT).show();
  155.         editor.putString(UserName, Password).commit();
  156.         Intent registerIntent = new Intent(this,MainActivity.class);
  157.         this.startActivity(registerIntent);
  158.  
  159.     }
  160. }
  161.  
  162.  
  163. Daf3 Java////////////////////////////////////
  164.  
  165. package com.mordekicloud.yuli.user123;
  166.  
  167. import android.app.Activity;
  168. import android.content.Intent;
  169. import android.content.SharedPreferences;
  170. import android.os.Bundle;
  171. import android.view.View;
  172. import android.widget.TextView;
  173.  
  174. /**
  175.  * Created by yuli.mordek on 3/11/2016.
  176.  */
  177. public class Daf3 extends Activity
  178. {
  179.     TextView txtUser1;
  180.     @Override
  181.     protected void onCreate(Bundle savedInstanceState) {
  182.         super.onCreate(savedInstanceState);
  183.         setContentView(R.layout.daf3);
  184.         txtUser1=(TextView)findViewById(R.id.Msg);
  185.         txtUser1.setText("Hiiiiiiiii!!"+getIntent().getStringExtra("userPut"));
  186.  
  187.     }
  188. public void btnOut(View v)
  189. {
  190.     SharedPreferences
  191.             pref=getApplicationContext().getSharedPreferences("user",MODE_PRIVATE);
  192.     SharedPreferences.Editor editor=pref.edit();
  193.     editor.remove("Logged").commit();
  194.     Intent outIntent=new Intent(this,MainActivity.class);
  195.     this.startActivity(outIntent);
  196.  
  197. }
  198.  
  199. }
  200.  
  201.  
  202. main activity XML///////////////////////////
  203.  
  204. <LinearLayout
  205.     android:layout_height="match_parent"
  206.     android:layout_width="match_parent"
  207.     android:orientation="vertical"
  208.     xmlns:android="http://schemas.android.com/apk/res/android">
  209.  
  210.     <EditText
  211.         android:layout_width="match_parent"
  212.         android:layout_height="wrap_content"
  213.         android:layout_marginTop="20dp"
  214.         android:layout_marginBottom="20dp"
  215.         android:layout_margin="15dp"
  216.         android:hint="User Name"
  217.         android:textSize="25sp"
  218.         android:gravity="center"
  219.         android:id="@+id/txtUser1"
  220.         android:inputType="text"
  221.         />
  222.     <EditText
  223.         android:layout_width="match_parent"
  224.         android:layout_height="wrap_content"
  225.         android:layout_marginBottom="20dp"
  226.         android:layout_margin="15dp"
  227.         android:textSize="25sp"
  228.         android:hint="Password"
  229.         android:gravity="center"
  230.         android:id="@+id/txtPass1"
  231.         android:inputType="textPassword"/>
  232.     <Button
  233.         android:layout_width="match_parent"
  234.         android:layout_height="wrap_content"
  235.         android:layout_margin="15dp"
  236.         android:layout_marginBottom="20dp"
  237.         android:text="Log In"
  238.         android:textSize="25sp"
  239.         android:onClick="btnLogIn"
  240.         />
  241.     <Button
  242.         android:layout_width="match_parent"
  243.         android:layout_height="wrap_content"
  244.         android:layout_margin="15dp"
  245.         android:text="Sing Up"
  246.         android:textSize="25sp"
  247.         android:onClick="btnSingUp"
  248.         />
  249.  
  250.     </LinearLayout>
  251.  
  252.  
  253. daf2 XML//////////////////////////////
  254.  
  255. <LinearLayout
  256.     android:layout_height="match_parent"
  257.     android:layout_width="match_parent"
  258.     android:orientation="vertical"
  259.     xmlns:android="http://schemas.android.com/apk/res/android">
  260.  
  261.     <EditText
  262.         android:layout_width="match_parent"
  263.         android:layout_height="wrap_content"
  264.         android:layout_margin="15dp"
  265.         android:layout_marginTop="20dp"
  266.         android:textSize="25sp"
  267.         android:hint="Enter User Name"
  268.         android:gravity="center"
  269.         android:id="@+id/UserName"
  270.         android:inputType="text"/>
  271.     <EditText
  272.         android:layout_width="match_parent"
  273.         android:layout_height="wrap_content"
  274.         android:layout_margin="15dp"
  275.         android:textSize="25sp"
  276.         android:hint="Password"
  277.         android:gravity="center"
  278.         android:id="@+id/Password"
  279.         android:inputType="text"/>
  280.     <EditText
  281.         android:layout_width="match_parent"
  282.         android:layout_height="wrap_content"
  283.         android:layout_margin="15dp"
  284.         android:hint="Confirm Password"
  285.         android:textSize="25sp"
  286.         android:gravity="center"
  287.         android:id="@+id/ConfPass"
  288.         android:inputType="text"/>
  289.     <Button
  290.         android:layout_width="match_parent"
  291.         android:layout_height="wrap_content"
  292.         android:layout_margin="15dp"
  293.         android:text="Enter"
  294.         android:textSize="25sp"
  295.         android:onClick="btnEnter"
  296.         />
  297.     <Button
  298.         android:layout_width="match_parent"
  299.         android:layout_height="wrap_content"
  300.         android:layout_margin="15dp"
  301.         android:text="Cancel"
  302.         android:textSize="25sp"
  303.         android:onClick="btnCancel" />
  304. </LinearLayout>
  305.  
  306. daf3 XML////////////////////////////////////
  307.  
  308. <LinearLayout
  309.     android:layout_height="match_parent"
  310.     android:layout_width="match_parent"
  311.     android:orientation="vertical"
  312.     xmlns:android="http://schemas.android.com/apk/res/android">
  313.  
  314.     <TextView
  315.         android:layout_width="match_parent"
  316.         android:layout_height="wrap_content"
  317.         android:id="@+id/Msg"/>
  318.     <Button
  319.         android:layout_width="match_parent"
  320.         android:layout_height="wrap_content"
  321.         android:gravity="center"
  322.         android:text="Logout"
  323.         android:textSize="25sp"
  324.         android:layout_margin="15dp"
  325.         android:onClick="btnOut"
  326.         />
  327.  
  328. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement