Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.mario.predavanje6;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- Button login;
- EditText username, password;
- User user = null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- login = findViewById(R.id.button);
- username = findViewById(R.id.et_username);
- password = findViewById(R.id.et_password);
- //spremiPodatke();
- login.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- logiraj(username.getText().toString(), Integer.parseInt(password.getText().toString()));
- }
- });
- ucitajPodatke();
- }
- public boolean logiraj(String un, int pw){
- if(user != null){
- if(user.username.equals(un) && user.password == pw){
- Toast.makeText(this, "Pozdrav " + username.getText().toString(), Toast.LENGTH_SHORT).show();
- return true;
- }
- else{
- Toast.makeText(this, "Login nije uspio", Toast.LENGTH_SHORT).show();
- }
- }
- return false;
- }
- // public void spremiPodatke(){
- // SharedPreferences sharedPreferences = getSharedPreferences("userSettings", Context.MODE_PRIVATE);
- // SharedPreferences.Editor editor = sharedPreferences.edit();
- //
- // editor.putString("userName", "Korisnik1");
- // editor.putInt("password", 12345);
- //
- // editor.apply();
- // }
- public void ucitajPodatke(){
- SharedPreferences sharedPreferences = getSharedPreferences("userSettings", MODE_PRIVATE);
- String usernName = sharedPreferences.getString("userName", null);
- int password = sharedPreferences.getInt("password", 0);
- if(usernName != null && password != 0){
- user = new User(usernName, password);
- }
- Log.d("Login", "Ucitani podatci su: " + usernName + " " + password);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement