Advertisement
Ivan_sjc

SharedPreferences

Nov 17th, 2020
763
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.59 KB | None | 0 0
  1.  
  2. ***** Main Activity *******
  3.  
  4.  
  5. package com.novoandroid.minhasanotaes;
  6.  
  7. import android.os.Bundle;
  8.  
  9. import com.google.android.material.floatingactionbutton.FloatingActionButton;
  10. import com.google.android.material.snackbar.Snackbar;
  11.  
  12. import androidx.appcompat.app.AppCompatActivity;
  13. import androidx.appcompat.widget.Toolbar;
  14.  
  15. import android.view.View;
  16. import android.view.Menu;
  17. import android.view.MenuItem;
  18. import android.widget.EditText;
  19.  
  20. public class MainActivity extends AppCompatActivity {
  21.  
  22.     private AnotacoesPreferencia preferencias;
  23.     private EditText editTextAnotacao;
  24.  
  25.     @Override
  26.     protected void onCreate(Bundle savedInstanceState) {
  27.         super.onCreate(savedInstanceState);
  28.         setContentView(R.layout.activity_main);
  29.  
  30.         preferencias = new AnotacoesPreferencia(getApplicationContext());
  31.  
  32.  
  33.                 FloatingActionButton fab = findViewById(R.id.fab);
  34.         fab.setOnClickListener(new View.OnClickListener() {
  35.             @Override
  36.             public void onClick(View view) {
  37.  
  38.                 String textoRecuperado = findViewById(R.id.editText).toString();
  39.                 if(textoRecuperado.equals("")){
  40.                     Snackbar.make(view, "Nenhum texto inserido", Snackbar.LENGTH_LONG).show();
  41.  
  42.                 }else{
  43.  
  44.                     preferencias.salvarAnotacao(textoRecuperado);
  45.                     Snackbar.make(view, "Anotação Salva com sucesso", Snackbar.LENGTH_LONG).show();
  46.                 }
  47.  
  48.             }
  49.         });
  50.  
  51.             // Recuperar Anotação
  52.  
  53.         String anotacao = preferencias.recuperarAnotacao();
  54.             if(!anotacao.equals(""))
  55.                 editTextAnotacao.setText(anotacao);
  56.  
  57.             }
  58.  
  59.  
  60.     }
  61.  
  62.  
  63. ********Anotacao Preferencia *********
  64.  
  65.  
  66. package com.novoandroid.minhasanotaes;
  67.  
  68. import android.content.Context;
  69. import android.content.SharedPreferences;
  70.  
  71. public class AnotacoesPreferencia {
  72.  
  73.     private final SharedPreferences.Editor editor;
  74.     private Context context;
  75.     private SharedPreferences preferences;
  76.     private final String NOME_ARQUIVO = "anotacao.preferencia";
  77.     private final String CHAVE_NOME = "nome";
  78.  
  79.  
  80.     public AnotacoesPreferencia(Context c){
  81.         this.context = c ;
  82.         preferences = context.getSharedPreferences(NOME_ARQUIVO,0);
  83.         editor = preferences.edit();
  84.  
  85.     }
  86.  
  87.  
  88.  
  89.     public void salvarAnotacao(String anotacao){
  90.         editor.putString(CHAVE_NOME,anotacao);
  91.         editor.commit();
  92.  
  93.  
  94.     }
  95.  
  96.     public String recuperarAnotacao(){
  97.         return preferences.getString(CHAVE_NOME,"");
  98.  
  99.  
  100.  
  101.     }
  102.  
  103.  
  104.  
  105. }
  106.  
  107.  
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement