Advertisement
Guest User

Untitled

a guest
Feb 28th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. package com.example.mario.predavanje6;
  2.  
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.SharedPreferences;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.util.Log;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.EditText;
  12. import android.widget.Toast;
  13.  
  14. public class MainActivity extends AppCompatActivity {
  15.  
  16. Button login;
  17. EditText username, password;
  18. User user = null;
  19.  
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_main);
  24.  
  25. login = findViewById(R.id.button);
  26. username = findViewById(R.id.et_username);
  27. password = findViewById(R.id.et_password);
  28.  
  29. //spremiPodatke();
  30.  
  31. login.setOnClickListener(new View.OnClickListener() {
  32. @Override
  33. public void onClick(View v) {
  34. logiraj(username.getText().toString(), Integer.parseInt(password.getText().toString()));
  35. }
  36. });
  37.  
  38. ucitajPodatke();
  39. }
  40.  
  41. public boolean logiraj(String un, int pw){
  42.  
  43. if(user != null){
  44.  
  45. if(user.username.equals(un) && user.password == pw){
  46. Toast.makeText(this, "Pozdrav " + username.getText().toString(), Toast.LENGTH_SHORT).show();
  47. return true;
  48. }
  49. else{
  50. Toast.makeText(this, "Login nije uspio", Toast.LENGTH_SHORT).show();
  51. }
  52. }
  53.  
  54. return false;
  55.  
  56. }
  57.  
  58. // public void spremiPodatke(){
  59. // SharedPreferences sharedPreferences = getSharedPreferences("userSettings", Context.MODE_PRIVATE);
  60. // SharedPreferences.Editor editor = sharedPreferences.edit();
  61. //
  62. // editor.putString("userName", "Korisnik1");
  63. // editor.putInt("password", 12345);
  64. //
  65. // editor.apply();
  66. // }
  67.  
  68. public void ucitajPodatke(){
  69. SharedPreferences sharedPreferences = getSharedPreferences("userSettings", MODE_PRIVATE);
  70.  
  71. String usernName = sharedPreferences.getString("userName", null);
  72. int password = sharedPreferences.getInt("password", 0);
  73.  
  74. if(usernName != null && password != 0){
  75. user = new User(usernName, password);
  76. }
  77.  
  78. Log.d("Login", "Ucitani podatci su: " + usernName + " " + password);
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement