How to create a button to add one more to the editText value?

Asked

Viewed 25 times

-1

I’d like a little support from you... I’m trying to add a button to subtract and include the amount of items...

Look at my code... I’ve tried some ways, but without success. It has to be through Edittext

package com.example.trabalhohomecompras;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class Linha extends AppCompatActivity {

    int numero = 0;
    //private Button btnMais1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.linha);

        Button btnMais1 = findViewById(R.id.btnMais1);
        btnMais1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                numero = numero + 1; // adiciona +1 na váriavel numero
                TextView t = (TextView) findViewById(R.id.textView_teste);

                t.setText(String.valueOf(numero));
                System.out.print(numero);

                //TextView.setText(Integer.parseInt(String.valueOf(numero))); // coloca o valor na textview
                //EditText simpleEditText =  findViewById(R.id.edit_cont);

                //System.out.println(numero);
                //simpleEditText.setText(Integer.parseInt(String.valueOf(numero)));
                //simpleEditText.setText(numero);
            }

        });


    }
}

print

3 answers

0

package com.example.trabalhohomecompras;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class Linha extends AppCompatActivity {

    int numero = 0;
    //private Button btnMais1;
    private Button btnMais1;
    private EditText txtQuantidade;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.linha);

        //EditText qtd;
        txtQuantidade = (EditText) findViewById(R.id.edit_cont);
        btnMais1 = findViewById(R.id.btnMais1);
        TextView t = (TextView) findViewById(R.id.txt_Descricao);
        btnMais1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                numero = numero + 1; // adiciona +1 na váriavel numero
                //TextView t = (TextView) findViewById(R.id.textView_teste);
                txtQuantidade.setText(Integer.parseInt(String.valueOf(numero)));
                t.setText(String.valueOf(numero));
                System.out.print(numero);

                //TextView.setText(Integer.parseInt(String.valueOf(numero))); // coloca o valor na textview
                //EditText simpleEditText =  findViewById(R.id.edit_cont);

                //System.out.println(numero);
                //simpleEditText.setText(Integer.parseInt(String.valueOf(numero)));
                //simpleEditText.setText(numero);
            }

        });


    }

}

I tried several ways, but I didn’t...

0

Every screen component must be assigned in the method onCreate() using findViewById (there are more modern methods, but don’t worry about it now).

For +1 assignment use the operator ++.

t.setText expecting a string, and number is int. Perform the conversion using String.valueOf, for example.

In Java, do not perform the conversion inside the t.setText() he usually complains about it.

public class Linha extends AppCompatActivity {

    int numero = 0;
    //private Button btnMais1;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.linha);
    
    // Todos elemento de tela que irá fazer uso findViewById deve ser declarado dentro do onCreate
    
    TextView t = (TextView) findViewById(R.id.textView_teste);
        Button btnMais1 = findViewById(R.id.btnMais1);
        
    btnMais1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
        
        // Add +1 em numero
        numero++;
        
        // Converter Int em String
        String converterNumero = String.valueOf(numero);
                
        //Informe o novo numero.
        t.setText(converterNumero);
                
            }
    
        });
    
    
    }
}
  • By doing this you guided me there returns that I must create a Gett for the variable t.setText

  • t. setText(converterNumero); In this part of the code it states that you must "Extract Method" ai of the two options of making Gett out of onClik or inside it.

0

If your idea is to update the values You need to do the findViewById()` of Edittext to pick up your reference.

And you will make the updates in this Edittext.

    public class Linha extends AppCompatActivity {
    
        int numero = 0;
        private Button btnMais1;
        private EditText txtQuantidade;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.linha);

          EditText quantidade = (EditText) findViewById(R.id.edit_text_quantidade);

          Button btnMais1 = findViewById(R.id.btnMais1);
          btnMais1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                numero = numero + 1; // adiciona +1 na váriavel numero
                quantidade.setText(String.valueOf(numero));

                System.out.print(numero);
            }

        });


    }
}
  • I tried to implement it that way...

  • It seems that does not work onclik, or varialvel or edit do not receive anything in my code...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.