How to display an entire variable in a textView and add +1 to it each time I click on a button present in the same Activity?

Asked

Viewed 2,642 times

0

I would like textView to display the value of the variable I created in Mainactivity "int numero = 0".

package genesysgeneration.a10;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    int numero = 0;
    private Button btnMais1;

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

        btnMais1=(Button)findViewById(R.id.btnMais1);

    }
}

However I could not do it, so I put 0 to appear something in the emulator image and make it easier to understand what I would like to do.

inserir a descrição da imagem aqui

I would like when clicking the button the value of the variable increased by 1 and the textView was updated.

I would also like to know if it was necessary to put private or public before declaring the integer variable int, type:

private int numero = 0;

or

public int numero = 0;

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="genesysgeneration.a10.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/tvContador"
        android:text="0" />

    <Button
        android:text="+ 1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/btnMais1" />
</RelativeLayout>

1 answer

0


Just use the method OnClick() of button. To use this method, you need to apply a Onclicklistener in his Button.

package genesysgeneration.a10;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    int numero = 0;
    private Button btnMais1;

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

        btnMais1 =(Button) findViewById(R.id.btnMais1);
        btnMais1.setOnClickListener(maisHandle);
    }

    View.OnClickListener maisHandle = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             numero += 1; // adiciona +1 na váriavel numero
             TextView.setText(String.valueOf(numero)); // coloca o valor na textview
         }
     }
}

Use the variable numero as public or private does not interfere exactly with what you want to do. These are visibility levels or to be more summarized, says who can access or not.

Private: The class that created it is the only one that can access it.

Public: All classes can access this variable.

This means that if the variable numero was defined as public in his MainActivity all other classes could access it. Otherwise, only the MainActivity could access this variable.

  • THANK YOU SO MUCH BRO!!! VLW REALLY!!! Now if it’s not too much trouble, I wonder if I could put this public void outside of "@Override"?

  • look at the code now

  • THANK YOU!!! Soon I will try to do something similar to checkbox.

Browser other questions tagged

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