Doubts on Android

0

I'm trying to get what comes in the "edtcomprado" and if it already has something in tvmac it will already add up with what I have stored but I can not find any way to do it

'package com.lucas.myapplication;

import android.content.SharedPreferences;
import android.support.design.widget.TextInputEditText;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView tvmac;
    Button btnmaca;
    TextInputEditText edtusado,edtcomprado;
    final String PREFS = "PREF";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvmac = findViewById(R.id.tvmac);
        btnmaca = findViewById(R.id.btnmaca);
        edtusado = findViewById(R.id.edtusado);
        edtcomprado = findViewById(R.id.edtcomprado);
        ///salvar oque eu colocar na chave
        final SharedPreferences preferences = getSharedPreferences(PREFS,MODE_PRIVATE );


        ///quando eu clicar no botão "ok"
        btnmaca.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ///pra salvar oq eu digitar
                SharedPreferences.Editor editor = preferences.edit();
                editor.putString("comprado",edtcomprado.getText().toString());
                editor.putString("usado",edtusado.getText().toString());
                ///commit salva enquanto as tarefas estão rodando, porém é mais lento que o apply();
                editor.commit();
                subtracao();
            }

        });

    }
    public void subtracao(){
        int num1 = Integer.parseInt(edtcomprado.getText().toString());
        int num2 = Integer.parseInt(edtusado.getText().toString());
        int calc = num1 - num2;
        tvmac.setText(""+calc);
    }

}'
    
asked by anonymous 29.04.2018 / 19:41

0 answers