Almacenamiento y recuperación de información,

 package com.example.random;


import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.inputmethodservice.Keyboard;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
private TextView TbAcumulado,TbMensaje;
private EditText Et1;

@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TbAcumulado = (TextView)findViewById(R.id.TbAcumulado);
TbMensaje = (TextView)findViewById(R.id.TbMensaje);

Et1 = (EditText)findViewById(R.id.Et1);

SharedPreferences prefe = getSharedPreferences("datos", Context.MODE_PRIVATE);//primero es el archivo donde se almacena el dato
//variable para recojer el editText
String Valor = String.valueOf(prefe.getInt("Numero",0));
TbAcumulado.setText(Valor);



}
public void Ingresar (View view){
int valor = Integer.parseInt(Et1.getText().toString());
if (valor > 0){
int Numero = Integer.parseInt(TbAcumulado.getText().toString());

TbAcumulado.setText(String.valueOf(Numero));
TbMensaje.setText("Guardar numero de celular");
Et1.setText("");
//editor de la variable almacenada
SharedPreferences prefe = getSharedPreferences("datos",Context.MODE_PRIVATE);
SharedPreferences.Editor EDITOR = prefe.edit();
EDITOR.putInt("Numero",Numero);
EDITOR.commit();
}
else{

TbMensaje.setText("LLena la informacion");

}
}

Comentarios